加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

Global.asax中的ASP.NET会话

发布时间:2020-12-16 04:36:09 所属栏目:asp.Net 来源:网络整理
导读:Global.asax中的Application_AuthenticateRequest方法中的会话始终为null.我已经尝试过: this.Session,HttpContext.Current.Session 总是空的. protected void Application_AuthenticateRequest() { string userRole = string.Empty; if (Request.IsAuthent
Global.asax中的Application_AuthenticateRequest方法中的会话始终为null.我已经尝试过:
this.Session,HttpContext.Current.Session

总是空的.

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});
        }
    }

我也尝试使用Cache,但它不起作用,因为我需要每个用户的唯一信息.

如何在Global.asax中使用Session?HttpApplication Application属性是否对每个用户都是唯一的?

解决方法

您现在无法在请求生命周期中使用Session,它不可用/已填充,如果您想使用它,您需要在生命周期的后期移动到事件,例如 PostAcquireRequestState.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读