Application_AuthenticationRequest中设置的Thread.CurrentPrincipal不会在稍后的应用程序中设置

StackOverflow https://stackoverflow.com/questions/809727

  •  03-07-2019
  •  | 
  •  

在Application_AuthenticationRequest的global.asax文件中,我将Thread.CurrentPrincipal设置为自定义主体。我还将HttpContext.Current.User设置为相同的主体。

但是稍后在应用程序中,当我需要将Thread.CurrentPrincipal强制转换为自定义类型时,我收到运行时错误说: 无法将类型为“System.Web.Security.RolePrincipal”的对象强制转换为“OurCustomPrincipal”。

如何将Thread.CurrentPrincipal重置为RolePrincipal,更重要的是如何将它保存在我们在global.asax中设置的CustomPrincipal

提前致谢

有帮助吗?

解决方案

您现在肯定已经解决了问题但是以防万一,如果您使用的是ASP.NET的RoleProvider,则RoleManagerModule会覆盖FormsAuthenticationModule创建的GenericPrincipal对象,并在PostAuthenticateRequest期间将其替换为RolePrincipal对象: http://www.asp.net/Learn/Security/tutorial-11 -vb.aspx

其他提示

总而言之,快速解决方法是在Application_OnPostAuthenticateRequest处理程序上执行主体和身份替换。

请确认您已为IIDentity& Iprincipal接口然后你使用类似下面的代码来分配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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top