在ASP.Net自定义缓存校长 [英] Custom Principal Caching in ASP.Net

查看:90
本文介绍了在ASP.Net自定义缓存校长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功地与ASP.NET自定义主要的工作,但我的自定义主体的负载数据库中的数据,我不希望这种事情发生在每次请求到系统中。

我打开我的自定义主体在Application_OnPostAuthenticateRequest的Global.ascx文件,但它似乎会话状态不可用在这里。我在哪里假设第一个请求进入系统后,坚持下去,这样我就不必继续从头开始重新呢?

 保护无效Application_OnPostAuthenticateRequest(对象发件人,EventArgs的发送)
        {
            主要的IPrincipal = HttpContext.Current.User;            如果(principal.Identity.IsAuthenticated ==真&放大器;&安培; principal.Identity.AuthenticationType ==联合会)
            {
                CustomPrincipal customPrincipal = NULL;
                    IClaimsPrincipal claimsPrincipal =(ICla​​imsPrincipal)负责人;                    如果(claimsPrincipal == NULL)
                    {
                        抛出新ApplicationException的(我们应该有在这一点上声明主体);
                    }                    customPrincipal =新CustomPrincipal(claimsPrincipal);
                HttpContext.Current.User = customPrincipal;
                = Thread.CurrentPrincipal中customPrincipal;            }
        }


解决方案

使用缓存,而不是会议

I am successfully working with a custom principal in ASP.NET, but my custom principal loads database data and I don't want that happening on every request into the system.

I load my custom principal in the Global.ascx file in the Application_OnPostAuthenticateRequest, but it appears session state is not available here. Where am I suppose to stick it after the first request into the system so that I don't have to keep recreating it from scratch?

 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;

            }
        }

解决方案

Use the Cache instead of session

这篇关于在ASP.Net自定义缓存校长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆