Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Session in Global.asax

Tags:

asp.net

Session in Application_AuthenticateRequest method in Global.asax is always null.Ive already try:

this.Session,HttpContext.Current.Session 

always null.

  protected void Application_AuthenticateRequest()
    {
        string userRole = string.Empty;

        if (Request.IsAuthenticated)
        {
            if (this.Session["UserRole"] == null)
            {
               InsertSessionValue();
            }
            userRole =Session["UserRole"].ToString();
            HttpContext.Current.User = new GenericPrincipal(User.Identity, new string[] {userRole});
        }
    }

Ive also try to use Cache,but it doesnt work because i need unique information for each user.

How to use Session in Global.asax?Is HttpApplication Application property unique for each user?

like image 682
ozsenegal Avatar asked Sep 13 '10 00:09

ozsenegal


1 Answers

You just can't use Session at this point in the request lifecycle, it isn't available/populated yet, if you want to use it you'll need to move to an event later in the lifecycle, for example PostAcquireRequestState.

like image 188
Nick Craver Avatar answered Nov 12 '22 23:11

Nick Craver