2011-03-08 54 views
9

該站點正在我的本地IIS 6.1上運行。我想添加一些功能來從我們的AD中獲取信息。我的AD代碼適用於許多其他項目和我的開發服務器。下面是我嘗試在寫出來的用戶名:我得到如何獲取當前用戶的windows身份

Response.Write("1. " + this.Request.LogonUserIdentity.Name); 
Response.Write("2. " + Request.ServerVariables["Auth_User"]); 
Response.Write("3. " + WindowsIdentity.GetCurrent().Name.ToString()); 

的結果是:

  1. NT AUTHORITY \ IUSR
  2. 管理員
  3. NT AUTHORITY \ NETWORK SERVICE

如何獲取實際的Windows用戶名,例如我們的域名/用戶名

感謝

回答

2

此片段展示了LogonUserIdentity是如何設置(使用反射鏡)

if ((this._wr is IIS7WorkerRequest) && (((this._context.NotificationContext.CurrentNotification == RequestNotification.AuthenticateRequest) && !this._context.NotificationContext.IsPostNotification) || (this._context.NotificationContext.CurrentNotification < RequestNotification.AuthenticateRequest))) 
     { 
      throw new InvalidOperationException(SR.GetString("Invalid_before_authentication")); 
     } 
     IntPtr userToken = this._wr.GetUserToken(); 
     if (userToken != IntPtr.Zero) 
     { 
      string serverVariable = this._wr.GetServerVariable("LOGON_USER"); 
      string str2 = this._wr.GetServerVariable("AUTH_TYPE"); 
      bool isAuthenticated = !string.IsNullOrEmpty(serverVariable) || (!string.IsNullOrEmpty(str2) && !StringUtil.EqualsIgnoreCase(str2, "basic")); 
      this._logonUserIdentity = CreateWindowsIdentityWithAssert(userToken, (str2 == null) ? "" : str2, WindowsAccountType.Normal, isAuthenticated); 
     } 

,你可以看到這已經改變了IIS 7 我相信你正在使用Windows身份驗證+模擬所以我會去最後一個WindowsIdentity.GetCurrent()),我相信是運行的身份請求。

15

這裏有兩個不同的窗口用戶 - 第一個是您的應用程序用戶,第二個是您的ASP.NET應用程序(從IIS角度看,應用程序池)正在運行的用戶(或Windows帳戶)。 WindowsIdentity.GetCurrent通常會返回此參考。

要獲得使用該應用程序的實際Windows用戶,必須強制進行身份驗證。爲此,您可以在IIS中爲該網站啓用集成身份驗證(Windows身份驗證)。還要修改您的ASP.NET配置以使用Windows身份驗證。現在您可以使用HttpContext.Current.User.Identity來獲取實際用戶。

-3

用戶是否使用他們的AD域/用戶名登錄到站點,如果是這樣的話,抓住用戶名/域名?

如果沒有,你將無法得到這個,如果一個隨機網站可以抓住當前用戶登錄的域名/用戶名,這將是一個巨大的安全問題不是嗎?

3

HttpContext.Current.User.Identity可能對您有用。

同樣System.Threading.Thread.CurrentPrincipal可以提供幫助。

但情況可能是您實際上必須在用戶登錄時設置身份實例(儘管不一定實現IPrincipal及其周圍機制,而使用內置的WindowsIdentity實現)。

我不是100%在這個,Windows身份驗證可能自動設置爲您簡單檢索。

此外,檢查出this link from MSDN描述基本用戶操作,

0

首先,確保該網站是Intranet區域(如剛剛http://servername/而非http://www.servername.com/)之內,或者您需要將FQDN添加到受信任的IE網站安全設置。

其次,如果您不使用Intranet區域,請確保IE正在發送證書 - 工具 - > Internet選項 - >安全性 - >自定義級別 - >用戶身份驗證 - >登錄 - >使用當前用戶名和自動登錄密碼

相關問題