天天看點

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>