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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With