Application_AuthenticationRequestで設定されたThread.CurrentPrincipalは、アプリで後で設定されません
-
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ハンドラーでプリンシパルとIDの置換を実行することです。
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