2014-03-04 30 views
1

我環顧四周,仍然沒有得到答案。ASP.Net在全局文件裏面得到userid

我有一個ASP.NET應用程序,它使用Windows身份驗證。 web.config中的配置是否正確:

<authentication mode="Windows"/> 
    <authorization> 
     <deny users="?"/> 
    </authorization> 

我得到在方法的用戶ID: WindowsAuthentication_Authenticate(對象發件人,WindowsAuthenticationEventArgs E)

被獲取的用戶ID的代碼:

var userId = String.Empty; 
     //Dispalys the UserName from AD. 
     System.Security.Principal.IPrincipal User; 
     User = System.Web.HttpContext.Current.User; 
     if (User != null) 
     { 
      string username = User.Identity.Name; 
      username = username.Substring(username.IndexOf("\\") + 1); 

      CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture; 
      TextInfo textInfo = cultureInfo.TextInfo; 
      userId = (textInfo.ToTitleCase(username)); 
     } 

在這種情況下,System.Web.HttpContext.Current.User始終爲空。

我在這裏做錯了什麼?

回答

2

WindowsAuthentication_Authenticate方法的參數之一是WindowsAuthenticationEventArgs類型。其中一個屬性是Identity,因此您應該可以通過編寫e.Identity.Name來訪問用戶名。

參考鏈接: