1

这是我得到的错误“会话状态在此上下文中不可用。”

只是一个业余程序员,我如何在 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)
    {

    }
}

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

4

1 回答 1

3

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

会话应该在这个阶段可用

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

于 2013-02-28T12:05:17.437 回答