Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Session on all actions of controller

Tags:

c#

asp.net-mvc

I have an simple MVC app I want to check first the session as this action

 public ActionResult Index()
        {
            if (Session["UserInfo"] == null)
            {
              return  RedirectToAction("Login", "Users");
            }
            return View();
        }

My question is about is there a way to enforce this check to all actions without do it manual for each action?

like image 667
freelancer Avatar asked Mar 15 '17 18:03

freelancer


People also ask

How can controller get the session ID?

The Controller consists of the following two Action methods. Inside this Action method, simply the View is returned. When the Get SessionId Button is clicked, SetSession Action method is executed which saves the value to the Session using the SetString method. Then the Session ID is retrieved from HttpContext.

How check session expired in asp net core?

In asp.net, It is very simple to detect session time out and redirect the user to login page or home page. All you have to do is, specify the redirection page in session_start event handler in Global. asax file as shown below. If the session has timed out, the user will be redirected to the login page.

What is session in MVC controller?

In MVC the controller decides how to render view, meaning which values are accepted from View and which needs to be sent back in response. ASP.NET MVC Session state enables you to store and retrieve values for a user when the user navigatesto other view in an ASP.NET MVC application.

What are controller actions?

An action (or action method) is a method on a controller which handles requests. Controllers logically group similar actions together. This aggregation of actions allows common sets of rules, such as routing, caching, and authorization, to be applied collectively. Requests are mapped to actions through routing.


1 Answers

you can use OnActionExecuting and can also override this method to write custom logic so create a class

 public class SessionCheck: ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpSessionStateBase session = filterContext.HttpContext.Session;
            if (session != null && session["UserInfo"] == null)
            {
                filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary {
                                { "Controller", "Users" },
                                { "Action", "Login" }
                                });
            }
        }
    } 

add namespaces in the class

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

and in your controller add [SessionCheck] attribute like this

 [SessionCheck]
 public class HomeController : Controller
  {
  }

this will check session on all the actions of controller or you can also add this attribute on action like this

[SessionCheck]
public ActionResult Index()
 {
 } 
like image 179
Usman Avatar answered Dec 07 '22 22:12

Usman