Showing posts with label Session. Show all posts
Showing posts with label Session. Show all posts

Session Authoraization in MVC

If you are using Session Authorization in your MVC Application then you need to check the existence of the session in each controller method.
In order to avoid this, you can make your own attribute that inherits from AuthorizeAttribute, and by overriding AuthorizeCore and HandleUnauthorizedRequest methods. You can place this attribute on the needed controllers.

Following is the code for the Custom Attribute to check the session

AuthorizeSessionAttribute.cs
public class AuthorizeSessionAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        return httpContext.Session["UserID"] != null;
    }

    protected override void HandleUnauthorizedRequest(AuthorizationContext 

filterContext)
    {
        filterContext.Result = new RedirectResult("/account/login");
    }
}
Usage:

In a controller method
public class SomeController : Controller
{
    [AuthorizeSession]
    public ActionResult Index()
    {
    }
}
In a controller
[AuthorizeSession]
public class SomeController : Controller
{
    public ActionResult Index()
    {
    }
}
Here it will applies to all the methods in the controller.
If you want a single method need to by pass this session Authorization then you need to put the [AllowAnonymous] attribute to that method as below
[AuthorizeSession]
public class SomeController : Controller
{
    public ActionResult Index()
    {
    }

    [AllowAnonymouse]
    public ActionResut publicpage()
    {
    }
}

Different Session Timeout per each user

Using Session.Timeout in code behind will set the timeout on a per user basis. Setting SessionState Timeout in Web.Config is for all users.

So for each user we can Set the Session timeout in Session_Start method like below

//Gets the usertimeout preference from database
int usertimeout=GetUserTimeOut(HttpContext.Current.Session["UserId"]);
//Change the session timeout for the current session
HttpContext.Current.Session.Timeout= usertimeout

Quick Overview of ASP.NET Sessions Mechanism

ASP.NET session state is maintained by using one of two underlying mechanisms.

The first is by using HTTP cookies. The idea behind HTTP cookies is that when the client sends a request, the server sends back a response with an HTTP Set-Cookie header that has a name/value pair in it. For all subsequent requests to the same server, the client sends the name/value pair in an HTTP Cookie header. The server then can use the value to associate the subsequent requests with the initial request. ASP.NET uses a cookie that holds a session ID to maintain session state. Then that ID is used to find the corresponding instance of the HttpSessionState class for that particular user. The HttpSessionState class provides just a generic collection in which you can store any data that you want.


The other mechanism for maintaining session state works without cookies. Some browsers do not support cookies or are not configured to keep and send cookies. ASP.NET provides a mechanism for getting around this problem by redirecting a request to a URL that has the ASP.NET session ID embedded in it. When a request is received, the embedded session ID is simply stripped out of the URL and is used to find the appropriate instance of the session object. This works great for browsers that are doing HTTP GET requests, but creates issues when writing Microsoft® .NET code that consumes an XML Web service.