Carlo Adap
Carlo Adap

Reputation: 257

Retrieving Session in Global.asax

This is the error I get "Session state is not available in this context."

Just an amateur programmer, how can I retrieve Session in Global.asax

this is my code in Global.asax

public class Global : System.Web.HttpApplication
{
    protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e)
    {
        if (FormsAuthentication.CookiesSupported == true)
        {
            if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
            {
                try
                {            
                    string Email = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                    string Roles = LoginVerification.GetUserRoles(Email);
                    string DisplayName = (string)Session["DisplayName"];
                    e.User = new System.Security.Principal.GenericPrincipal(
                    new System.Security.Principal.GenericIdentity(DisplayName, "Forms"), Roles.Split(';'));
                }
                catch (Exception)
                {

                }
            }
        }
    }

    protected void Application_Start(object sender, EventArgs e)
    {

    }

    protected void Session_Start(object sender, EventArgs e)
    {

    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {

    }

    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {

    }

    protected void Application_Error(object sender, EventArgs e)
    {

    }

    protected void Session_End(object sender, EventArgs e)
    {

    }

    protected void Application_End(object sender, EventArgs e)
    {

    }
}

Thank you in advance for answering my question! :)

Upvotes: 1

Views: 5226

Answers (1)

jbl
jbl

Reputation: 15413

I guess this code should be in a AcquireRequestState event handler instead of AuthenticateRequest event handler.

Session should be available at this stage

see https://stackoverflow.com/a/9501213/1236044

Upvotes: 3

Related Questions