2013-04-24 126 views
17

我有一個使用ASP.NET窗體身份驗證的ASP.NET 3.5應用程序。當在頁面中編輯數據時,我希望能夠獲取當前登錄到計算機的Windows用戶名(不登錄到ASP.NET應用程序,但登錄到Windows)。如何從ASP.NET頁面獲取當前登錄的Windows帳戶?

如果我使用Context.User.Identity.Name.Tostring(),我得到登錄到ASP.NET應用程序的用戶名,但我需要Windows帳戶名。

System.Security.Principal.WindowsIdentity.GetCurrent().Name.Tostring() 

而且,只有當我從Visual Studio所經營的網站工作,但部署到IIS後返回NT AUTHORITY \ SYSTEM

+1

使用Windows身份驗證。否則,瀏覽器將如何知道,以及爲什麼它會向服務器發送Windows用戶登錄的內容? – CodeCaster 2013-04-24 06:21:36

+0

@CodeCaster,我應該採取它完全不可能使用窗體身份驗證?應用程序使用角色來控制訪問級別,但我希望獲得當前的Windows帳戶以進行一些後端試聽 – StackTrace 2013-04-24 06:26:10

+0

角色沒有綁定到特定的身份驗證方法,您也可以在Windows身份驗證中使用它們。這是一個Intranet應用程序嗎? – CodeCaster 2013-04-24 07:09:47

回答

10

您必須在您的配置中將認證模式設置爲Windows &也禁用授權標籤中的匿名用戶。

+0

嗨,我有兩個模塊在同一個項目,即客戶端和管理模塊。客戶端網站有登錄頁面和管理模塊使用windowsAuth模式..如果我在.config中設置模式,那麼它會影響客戶端模塊? – user3217843 2017-04-27 12:58:41

+0

如何禁用授權標籤中的匿名用戶? – 2018-02-21 12:32:02

0

嘗試用下面的一行代碼:

string loggedOnUser = string.Empty; 
loggedOnUser = Request.ServerVariables.Get("AUTH_USER"); 

當您從Visual Studio中的應用程序,您可能不會得到的數值...檢查它部署在IIS之後。

用於獲取用戶名,使用方法:

string userName = string.Empty; 
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "Your Domain Name")) 
{ 
    UserPrincipal user = new UserPrincipal(pc); 
    user = UserPrincipal.FindByIdentity(pc, "User ID Will Come here"); 
    if (user != null) 
    { 
     userName = user.GivenName + " " + user.Surname; 

    } 
    else 
    { 
     //return string.Empty; 
     userName = "User Not Found"; 
    } 
} 
8

獲取當前登錄的用戶的Windows帳戶必須使用Windows authentication而不是Forms authentication

System.Security.Principal .WindowsIdentity.GetCurrent()。Name.Tostring() 也只適用於當我從Visual Studio運行網站,但 部署到IIS後它返回NT AUTHORITY \ SYSTEM

它顯示應用程序當前用戶。當您將應用程序託管在Visual Studio Web服務器上時,它將使用您的本地帳戶。但是,當您使用不同的憑據登錄到Web應用程序時,它將始終顯示您當前的Windows登錄名。

部署到IIS的應用程序在您的情況下使用NT AUTHORITY \ SYSTEM帳戶。

4

我用這個:

System.Security.Principal.WindowsPrincipal user; 
user = new WindowsPrincipal(this.Request.LogonUserIdentity); 
this.Request.LogonUserIdentity.Impersonate(); 
user_name = user_name.Substring(user_name.LastIndexOf("\\") + 1); 
4

獲取當前登錄的用戶到Windows在C#中,使用:

string Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(); 
1

我掙扎着,掙扎着,並與此掙扎。其中一件事是我沒有訪問IIS的權限,因此我無法更改任何服務器設置。我不得不按照我在代碼方面的能力去做。當我研究它時,許多答覆都說:「像這樣設置IIS」。 。 。well,當你有權訪問IIS時,這很好,但我沒有 - 我不得不在代碼中做什麼。所以,我最終處理這樣的:

在我的網絡配置文件,我添加下面的代碼行 部分中:

<system.webServer> 
<security> 
    <authentication> 
    <anonymousAuthentication enabled="false" /> 
    <windowsAuthentication enabled="true" /> 
    </authentication> 
</security> 
</system.webServer> 

然後,它在我的本地返回一個錯誤,我必須進去修理。我去了位於我的機器上以下路徑的applicationhost.config文件(您的可能會有所不同):

C:\ users \「您的用戶名」\ My Documents \「yourIISInstallation」\ config \ applicationhost.config

,我改變了以下設置爲 「允許」,已被設置爲 「拒絕」:

<section name="anonymousAuthentication" overrideModeDefault="Deny" /> 

改爲

<section name="anonymousAuthentication" overrideModeDefault="Allow" /> 

<section name="windowsAuthentication" overrideModeDefault="Deny" /> 

<section name="windowsAuthentication" overrideModeDefault="Allow" /> 

<sectionGroup name="authentication"> 

部。在我發現這個問題之前,我把頭髮拉出來。我希望這可以幫助別人。只要我將上面的代碼放到webconfig文件中,它就在intranet上工作,它只是在本地返回錯誤,但只要我將上面的代碼添加到本地applicationhost.config文件中,它就開始工作在本地以及。然後,我調用以下變量以返回Windows上登錄用戶的名稱:

HttpContext.Current.User.Identity.Name.ToString().Substring((HttpContext.Current.User.Identity.Name.ToString().IndexOf("\\")) + 1); 

乾杯!

相關問題