2009-04-24 23 views
5

私は私のカスタムログインシーケンスの一部としてWeb.HttpContext.Current.User.Identity.Nameはどこから来たのですか?

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からusernameパラメータを取得するように見えます。

私の質問は.... auth cookieのユーザー名は、現在のユーザーIDの名前としていつ設定されますか?

答えて

3

であり、そのオブジェクトはの一環として、あなたの場合、おそらくSystem.Web.Security.FormsAuthenticationModuleには、標準のASP.NETのHttpModulesの1に設定されています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 
2

System.Web.Security.FormsAuthenticationModuleのプライベートメソッドOnAuthenticateのように見えます。行は、ユーザ名がIPrincipleユーザーオブジェクトのプロパティだけです

e.Context.SetPrincipalNoDemand(
     new GenericPrincipal(new FormsIdentity(ticket), 
     new string[0])); 
関連する問題