12

我想使用Windows身份驗證獲取用戶名如何在asp.net中使用Windows身份驗證獲取用戶名?

其實我實現了「以不同的用戶登錄」,當點擊這個按鈕時,Windows安全將出現在那裏,我們可以提供憑證。

在那段時間,如果我給一些其他證書,它只是取當前用戶名。 如何從Windows安全獲取給定的憑證用戶名?

在IIS中的主機應用程序,然後匿名身份驗證已禁用,並啓用Windows身份驗證。

的web.config:

<system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    <identity impersonate="true"/> 
    <authorization> 
     <allow users="*"/> 
     <deny users="*"/> 
    </authorization> 
</system.web> 
<system.webServer> 
    <directoryBrowse enabled="true" /> 
    <security> 
     <authentication> 
      <anonymousAuthentication enabled="false" /> 
      <windowsAuthentication enabled="true" /> 
     </authentication> 
    </security> 

的.cs

在這裏我得到了默認的用戶名始終

string fullName = Request.ServerVariables["LOGON_USER"]; 

任何想法?在此先感謝

回答

0

我認爲,因爲下面的代碼,你沒有得到新的憑證

string fullName = Request.ServerVariables["LOGON_USER"]; 

您可以嘗試自定義登錄頁面

+0

感謝您的迴應文章...如何使用自定義登錄頁面? – user2148679

+0

我的意思是設計新的登錄頁面。您可以嘗試使用Windows身份驗證的活動目錄身份驗證。你在哪個瀏覽器上運行你的頁面? –

21

您可以通過在Windows身份驗證得到了用戶的WindowsIdentity對象:

WindowsIdentity identity = HttpContext.Current.Request.LogonUserIdentity; 

,然後你可以得到關於像identity.Name用戶的信息。

請注意你需要有這些代碼的HttpContext。

+0

感謝您的回覆...我試過這個,但沒有工作..如果我給任何錯誤的密碼在Windows安全它正在如何解決這個問題? – user2148679

+2

你的意思是在Windows驗證對話框輸入錯誤的密碼?它不應該允許您通過身份驗證。 – slepox

4

這應該工作:

User.Identity.Name 

Identity返回IPrincipal

這裏是鏈接到Microsoft documentation

相關問題