ASP.Net中的自定义主体缓存

时间:2011-06-15 18:44:36

标签: asp.net

我在ASP.NET中成功使用自定义主体,但我的自定义主体加载数据库数据,我不希望每次进入系统的请求都发生这种情况。

我将自定义主体加载到Application_OnPostAuthenticateRequest中的Global.ascx文件中,但此处似乎没有会话状态。在第一次请求进入系统后,我想在哪里坚持下去,以便我不必从头开始重新创建它?

 protected void Application_OnPostAuthenticateRequest(Object sender, EventArgs e)
        {
            IPrincipal principal = HttpContext.Current.User;

            if(principal.Identity.IsAuthenticated == true && principal.Identity.AuthenticationType == "Federation")
            {
                CustomPrincipal customPrincipal = null;


                    IClaimsPrincipal claimsPrincipal = (IClaimsPrincipal)principal;

                    if (claimsPrincipal == null)
                    {
                        throw new ApplicationException("We should have a claims principal at this point");
                    }

                    customPrincipal = new CustomPrincipal(claimsPrincipal);


                HttpContext.Current.User = customPrincipal;
                Thread.CurrentPrincipal = customPrincipal;

            }
        }

1 个答案:

答案 0 :(得分:1)

使用缓存而不是会话