5

私は持っている

FormsAuthentication.SetAuthCookie("someName", True)

カスタム ログイン シーケンスの一部として。後で、特定の役割のみを許可するページがあります。

<location path="myPage.aspx">
    <system.web>
        <authorization>
            <allow roles="SomeRole"/>
            <deny users="*"/>
        </authorization>
    </system.web>
</location>

私の知る限り、これはロール プロバイダーの GetRolesForUser の実装を呼び出します。ユーザー名パラメーターを Web.HttpContext.Current.User.Identity.Name から取得しているようです。

私の質問は....認証Cookieのユーザー名が現在のユーザーIDの名前として設定されるのはいつですか?

4

2 に答える 2

4

ユーザー名はIPrincipleユーザーオブジェクトの単なるプロパティであり、そのオブジェクトは標準のASP.NET HTTPModuleの1つ、この場合はおそらくSystem.Web.Security.FormsAuthenticationModuleにOnAuthenticateメソッドの一部として設定されます。

別のユーザー名やIDの設定など、この情報を変更する方法を知りたい場合は、Application_AuthenticateRequestをオーバーライドするglobal.asaxまたはカスタムHTTPModuleの作成を検討する必要があります。次に例を示します。

Public Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim cookieName As String = FormsAuthentication.FormsCookieName
    Dim authCookie As HttpCookie = HttpContext.Current.Request.Cookies(FormsAuthentication.FormsCookieName)

    If Not IsNothing(authCookie) Then
        Dim authTicket As FormsAuthenticationTicket = FormsAuthentication.Decrypt(authCookie.Value)
        If IsNothing(authTicket) OrElse authTicket.Expired Then
            HttpContext.Current.Response.Redirect(FormsAuthentication.LoginUrl)
        Else
            Dim id As New FormsIdentity(authTicket)

            Dim newUser As New YourCustomUserType(id.Name)
            HttpContext.Current.User = newUser
        End If
    End If
End Sub
于 2009-04-24T20:24:46.750 に答える
2

System.Web.Security.FormsAuthenticationModule のプライベート メソッド OnAuthenticate で発生する可能性があるようです。ラインは

 e.Context.SetPrincipalNoDemand(
      new GenericPrincipal(new FormsIdentity(ticket),
      new string[0]));
于 2009-04-24T19:59:01.490 に答える