من أين web.httpcontext.current.user.dentity.name؟
-
16-09-2019 - |
سؤال
أملك
FormsAuthentication.SetAuthCookie("someName", True)
كجزء من تسلسل تسجيل الدخول المخصص. في وقت لاحق، لدي بعض الصفحة فقط السماح بدور معين:
<location path="myPage.aspx">
<system.web>
<authorization>
<allow roles="SomeRole"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
بقدر ما أستطيع أن أقول، فإن ذلك يدعو إلى تنفيذ مزود بدوري للمجموعات. يبدو أنه يحصل على معلمة اسم المستخدم من Web.httpcontext.current.user.imentity.name.name.
سؤالي هو.... متى يتم تعيين اسم المستخدم من ملف تعريف الارتباط المصدر كاسم في هوية المستخدم الحالية؟
المحلول
اسم المستخدم هو مجرد خاصية كائن مستخدم IPRINCIPLE ومحدد هذا الكائن في أحد أجهزة HttpModules ASP.NET القياسية، في حالتك ربما system.web.security.formsauthenticationModulemodule كجزء من الطريقة الصاخبة.
إذا كنت تريد معرفته هو كيفية تغيير هذه المعلومات، مثل تحديد اسم مستخدم أو هوية مختلفة، فسترغب في إلقاء نظرة على إنشاء Global.asax أو httpmodule مخصص يتجاوز التطبيق_Authenticaterequest. هنا مثال:
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
نصائح أخرى
يبدو أنه قد يحدث في الطريقة الخاصة onAuthenticate في system.web.security.formsauthenticationmodication. الخط هو
e.Context.SetPrincipalNoDemand(
new GenericPrincipal(new FormsIdentity(ticket),
new string[0]));