我有
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/