asp.net Context.User.Identity.Name說明


今天做了一個身份驗證頁面,基本實現功能,卻不能顯示當前用戶姓名,自己MSDN半天一無所獲,問題就在Context.User.Identity.Name;Context是HttpContext類,User屬性Gets or sets security information for the current HTTP request,再查identity是IIdentity類型的接口;Name是它的Public Properties,可以Gets the name of the current user。依舊木有辦法。網上的大蝦們說是驗證方式的問題,就知道Form方式,加到Web.cofig裏,直接搞定!!!!

////////////////////////////粘網絡資源,共享快樂

(1)在ASP.NET中有三種身份驗證方式。當使用其中的一種來驗證當前用戶並通過時,必然要在服務器及客戶端之間保存一種驗證票據來保存驗證信息。.NET提供類似於IIdentity的接口來完成該功能。請樓主注意,context.user是IPrincipal類型接口,而identity是IIdentity類型的接口。有四種類FormsIdentity GenericIdentity PassportIdentity WindowsIdentity實現了IIdentity接口。這幾種類對應於.NET的幾種驗證模式。以上也差不多是廢話,只是說了一下。 不過,請樓主看一下以上說的, 再對MSDN裏查找一下ASP.NET的Forms驗證的文章。看一下在forms驗證時對FormsIdentity類的使用,有些信息保存到FormsIdentity類的實例裏,在使用的時候就可以context.user.identity.name提取出來。

(2)Context.User.Identity.Name總是爲空,望高手指教爲什麼?
______________________________________________________________________________________________
答1:
are you using forms-based authentication or windows integrated authentication?
______________________________________________________________________________________________
答2:
你把asp.net改爲windows內置驗證,Context.User.Identity.Name就有值了
______________________________________________________________________________________________
答3:
up

附上:Web.config//只解決本問題,其他問題再說

<system.web>
      <authentication mode = "Forms">

</system.web>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章