在Global.asax中检索会话

时间:2013-02-28 11:23:34

标签: c# asp.net session

这是我得到的错误 “在这种情况下,会话状态不可用。”

只是一个业余程序员,如何在Global.asax中检索Session

这是我在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)
    {

    }
}

提前感谢您回答我的问题! :)

1 个答案:

答案 0 :(得分:3)

我想这段代码应该在AcquireRequestState事件处理程序中,而不是AuthenticateRequest事件处理程序。

会议应在此阶段提供

请参阅https://stackoverflow.com/a/9501213/1236044