Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Principal Caching in ASP.Net

Tags:

asp.net

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;

            }
        }
like image 405
ctrlalt313373 Avatar asked Jan 24 '26 14:01

ctrlalt313373


1 Answers

Use the Cache instead of session

like image 132
Yuriy Rozhovetskiy Avatar answered Jan 26 '26 04:01

Yuriy Rozhovetskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!