ozsenegal
ozsenegal

Reputation: 4143

ASP.NET Session in Global.asax

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?

Upvotes: 8

Views: 3281

Answers (1)

Nick Craver
Nick Craver

Reputation: 630627

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.

Upvotes: 13

Related Questions