2009-08-01 30 views
2

試圖弄清楚如何從ASP.Net應用程序中的BLL類中獲取當前用戶的全名,如同在Active Directory中輸入的一樣。到目前爲止,我有:從ASP.Net中的Windows身份驗證獲取全名BLL類

public static string Username 
    { 
     get 
     { 
      var name = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 

      if (name.Contains("\\")) 
      { 
       var start = name.IndexOf("\\"); 

       if (name.Length > start) 
       { 
        name = name.Substring(start + 1); 
       } 
      } 

      return name; 
     } 
    } 

這樣做的問題是,這將返回用戶名,但我也需要全名的。任何人都知道這是可能的嗎?

回答

3

使用DirectorySearcher ...

var search = new DirectorySearcher(new DirectoryEntry("LDAP://YourDomain")); 
search.Filter = "(sAMAccountName=UserNameHere)"; // put the identity name here 
var res = search.FindOne(); 
var name = res["displayName"]; // I believe this is the right property 
+0

Noooooes ......但我不想竟查詢AD! – 2009-08-01 01:32:18