asp.net - Web.HttpContext.Current.User.Identity.Name来自何处?

标签 asp.net forms-authentication httpcontext roleprovider

我有

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

最佳答案

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

如果您想知道如何更改此信息,例如设置其他用户名或身份,则需要查看创建一个覆盖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

关于asp.net - Web.HttpContext.Current.User.Identity.Name来自何处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/787143/

相关文章:

javascript - 如何使用 Google 图表 API 在 X 轴上使用日期?

c# - 如何在运行时在 asp.net 中的内容占位符中添加按钮?

c# - 如何将 VB.net 常量转换为 C#?

c# - 如何在表单例份验证中将 User.Identity.IsAuthenticated 的值设置为 false

asp.net - SP2010 - httpcontext.response.write() 不适用于 LinkBut​​ton 的 onClick 事件

c# - 如何获取 HttpContext 调用的 Web 服务方法?

c# - 如何在 FakeHttpContext 中设置 Request.Header 以进行单元测试

javascript - 如何使用 window.print 在打印预览中设置默认选中/启用的背景图形选项

asp.net-mvc - ASP.Net MVC - 使用外部 URL 进行表单例份验证

asp.net - 授权问题 - 匿名用户无法访问 .jpeg 或 .css