asp.net – Web.HttpContext.Current.User.Identity.Name来自哪
发布时间:2020-12-16 06:56:10 所属栏目:asp.Net 来源:网络整理
导读:我有 FormsAuthentication.SetAuthCookie("someName",True) 作为我的自定义登录序列的一部分.后来,我有一些页面只允许一个特定的角色: location path="myPage.aspx" system.web authorization allow roles="SomeRole"/ deny users="*"/ /authorization /sys
我有
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获取用户名参数. 我的问题是……来自auth cookie的用户名何时被设置为我当前用户身份中的名称? 解决方法
用户名只是IPrinciple用户对象的一个??属性,该对象在一个标准ASP.NET HTTPModules中设置,在您的情况下可能是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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- asp.net – 使用MVC4的Autofac:控制器没有默认构
- asp.net – 如何从搜索引擎中完全隐藏网站?
- asp.net-mvc – ASP.NET MVC 3 – 这个布尔值如何
- asp.net-core – Visual Studio 2017(ASP.NET Co
- 带有子项的ASP.NET自定义/用户控件
- asp.net – 如何从SQL数据库流.flv文件
- asp.net-mvc – 从单个Web服务器迁移到多个Web服
- entity-framework-4 – 当超出范围时,Ninject不调
- asp.net-mvc – 在MVC视图中,如果在表单标记中没
- asp.net-mvc-4 – 如果用户尝试通过URL访问其他网
热点阅读