2008-10-29 12 views
22

Piszę aplikację internetową, która korzysta z uwierzytelniania systemu windows i mogę szczęśliwie dostać nazwę logowania użytkownika przy użyciu coś jak:Jak znaleźć nazwę wyświetlaną Active Directory użytkownika w aplikacji internetowej C#?

string login = User.Identity.Name.ToString(); 

Ale nie muszę ich login Chcę ich DisplayName. Już od kilku godzin walę mi w głowę ...

Czy mogę uzyskać dostęp do AD mojej organizacji za pomocą aplikacji internetowej?

Odpowiedz

29

Jak o tym:

private static string GetFullName() 
    { 
     try 
     { 
      DirectoryEntry de = new DirectoryEntry("WinNT://" + Environment.UserDomainName + "/" + Environment.UserName); 
      return de.Properties["fullName"].Value.ToString(); 
     } 
     catch { return null; } 
    } 
+0

Jest to najlepszy sposób, aby zrobić to dla pojedynczych użytkowników. – thismat

+0

Tak, podoba mi się prostota tego ALOT, zwłaszcza, że ​​działa. Jestem ci winien drinka! – inspite

+0

To działało, ale właściwości WINNT: // są o wiele mniej kompresyjne niż właściwości LDAP: // (patrz http://www.rlmueller.net/Name_Attributes.htm), więc podczas gdy to działało, nie byłem w stanie uzyskać adresy e-mail użytkownika za pomocą WINNT: // wiążące – inspite

8

Zobacz powiązane pytanie: Active Directory: Retrieve User information

Zobacz również: Howto: (Almost) Everything In Active Directory via C# a dokładniej odcinek "Enumerate an object's properties".

Jeśli masz ścieżkę do podłączenia do grupy w domenie, co następuje fragment może być pomocne:

GetUserProperty("<myaccount>", "DisplayName"); 

public static string GetUserProperty(string accountName, string propertyName) 
{ 
    DirectoryEntry entry = new DirectoryEntry(); 
    // "LDAP://CN=<group name>, CN =<Users>, DC=<domain component>, DC=<domain component>,..." 
    entry.Path = "LDAP://..."; 
    entry.AuthenticationType = AuthenticationTypes.Secure; 

    DirectorySearcher search = new DirectorySearcher(entry); 
    search.Filter = "(SAMAccountName=" + accountName + ")"; 
    search.PropertiesToLoad.Add(propertyName); 

    SearchResultCollection results = search.FindAll(); 
    if (results != null && results.Count > 0) 
    { 
     return results[0].Properties[propertyName][0].ToString(); 
    } 
    else 
    { 
      return "Unknown User"; 
    } 
} 
2

Jest projekt CodePlex dla Linq to AD , Jeśli jesteś zainteresowany.

Jest również objęty w książce LINQ Unleashed for C# przez Paula Kimmela - używa on powyższego projektu jako punktu wyjścia.

nie związany z obu źródeł - Właśnie przeczytałem książkę niedawno

3

na wypadek gdyby ktoś dba udało mi się złamać ten jeden:

 /// This is some imaginary code to show you how to use it 

     Session["USER"] = User.Identity.Name.ToString(); 
     Session["LOGIN"] = RemoveDomainPrefix(User.Identity.Name.ToString()); // not a real function :D 
     string ldappath = "LDAP://your_ldap_path"; 
     // "LDAP://CN=<group name>, CN =<Users>, DC=<domain component>, DC=<domain component>,..." 


     Session["cn"] = GetAttribute(ldappath, (string)Session["LOGIN"], "cn"); 
     Session["displayName"] = GetAttribute(ldappath, (string)Session["LOGIN"], "displayName"); 
     Session["mail"] = GetAttribute(ldappath, (string)Session["LOGIN"], "mail"); 
     Session["givenName"] = GetAttribute(ldappath, (string)Session["LOGIN"], "givenName"); 
     Session["sn"] = GetAttribute(ldappath, (string)Session["LOGIN"], "sn"); 


/// working code 

public static string GetAttribute(string ldappath, string sAMAccountName, string attribute) 
    { 
     string OUT = string.Empty; 

     try 
     { 
      DirectoryEntry de = new DirectoryEntry(ldappath); 
      DirectorySearcher ds = new DirectorySearcher(de); 
      ds.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + sAMAccountName + "))"; 

      SearchResultCollection results = ds.FindAll(); 

      foreach (SearchResult result in results) 
      { 
       OUT = GetProperty(result, attribute); 
      } 
     } 
     catch (Exception t) 
     { 
      // System.Diagnostics.Debug.WriteLine(t.Message); 
     } 

     return (OUT != null) ? OUT : string.Empty; 
    } 

public static string GetProperty(SearchResult searchResult, string PropertyName) 
    { 
     if (searchResult.Properties.Contains(PropertyName)) 
     { 
      return searchResult.Properties[PropertyName][0].ToString(); 
     } 
     else 
     { 
      return string.Empty; 
     } 
    } 
+0

ten kod zapisał mój dzień. !!!! –

4

użyj:

string displayName = UserPrincipal.Current.DisplayName;

Powiązane problemy