Thread.CurrentPrincipal set to Application_AuthenticationRequest is not set later in the application

In the global.asax file for Application_AuthenticationRequest, I set Thread.CurrentPrincipal for the custom principal. I also install HttpContext.Current.User to the same main.

However, later in the application, when I need to apply Thread.CurrentPrincipal to our custom type, I get a runtime error: Unable to pass an object of type "System.Web.Security.RolePrincipal" to enter "OurCustomPrincipal".

How Thread.CurrentPrincipal got a reset value for RolePrincipal, and moreover, how can I save it in CustomPrincipal, which we set in global.asax

Thanks in advance

+6
asp.net-membership
source share
3 answers

You probably solved your problem by now, but just in case you use RoleProvider from ASP.NET, RoleManagerModule overwrites the GenericPrincipal object created by the FormsAuthenticationModule module and replaces it with a RolePrincipal object during PostAuthenticateRequest: http://www.asp.net /Learn/Security/tutorial-11-vb.aspx

+6
source share

To summarize, you can quickly fix your basic and identical substitutions in the Application_OnPostAuthenticateRequest handler.

+3
source share

Please make sure you implement the class for the IIDentity and Iprincipal interface, and then you use something like the following code to assign the currentprincipal.

Dim userIdentity As CustomIdentity userIdentity = New CustomIdentity(username, True,"forms", sessionId) Dim principal As New CustomPrincipal(userIdentity, arrRoles) HttpContext.Current.User = principal System.Threading.Thread.CurrentPrincipal = principal 
0
source share

All Articles