Web.HttpContext.Current.User.Identity.Name 从哪里来?

发布于 2024-07-17 06:03:26 字数 576 浏览 7 评论 0原文

我有

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 中的用户名设置为我当前用户身份中的名称?

I have

FormsAuthentication.SetAuthCookie("someName", True)

as part of my custom login sequence. Later, I have some page only allowing a specific role:

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

As far as I can tell, that makes a call to my role provider's implementation of GetRolesForUser. It appears to get the username parameter from Web.HttpContext.Current.User.Identity.Name.

My question is.... when does the username from the auth cookie get set as the Name in my current user identity?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再浓的妆也掩不了殇 2024-07-24 06:03:26

用户名只是 IPrinciple 用户对象的一个​​属性,该对象是在标准 ASP.NET HTTPModule 之一中设置的,在您的情况下可能是 System.Web.Security.FormsAuthenticationModule 作为 OnAuthenticate 方法的一部分。

如果您想知道如何更改此信息,例如设置不同的用户名或身份,您将需要创建一个 global.asax 或覆盖 Application_AuthenticateRequest 的自定义 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

The username is just a property of the IPrinciple user object and that object is set in one of the standard ASP.NET HTTPModules, in your case probably System.Web.Security.FormsAuthenticationModule as part of the OnAuthenticate method.

If what you want to know is how to change this information, such as setting a different username or identity, you will want to look at creating a global.asax or a custom HTTPModule which overrides the Application_AuthenticateRequest. Here is an example:

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
等待我真够勒 2024-07-24 06:03:26

看起来它可能发生在 System.Web.Security.FormsAuthenticationModule 中的私有方法 OnAuthenticate 中。 该行是

 e.Context.SetPrincipalNoDemand(
      new GenericPrincipal(new FormsIdentity(ticket),
      new string[0]));

Looks like it may occur in the private method OnAuthenticate in System.Web.Security.FormsAuthenticationModule. The line is

 e.Context.SetPrincipalNoDemand(
      new GenericPrincipal(new FormsIdentity(ticket),
      new string[0]));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文