Come posso ottenere l'unità organizzativa dell'utente connesso in C #?

StackOverflow https://stackoverflow.com/questions/296466

  •  08-07-2019
  •  | 
  •  

Domanda

Sto cercando un metodo per ottenere il percorso LDAP attivo di Active Direcory dell'utente attualmente connesso. per esempio.

LDAP://CN=john.smith,OU=UK,OU=Sales,DC=Company,DC=local
È stato utile?

Soluzione

Interroga la directory LDAP (ad es. AD) con questo filtro:

(&(objectCategory=user)(sAMAccountName=<user-logon-name-here>))

Il DN dell'oggetto restituito è ciò che stai cercando.

Qualcosa del genere:

DirectorySearcher ds = new DirectorySearcher();
string userName = WindowsIdentity.GetCurrent().Name;
string userFilter = "(&(objectCategory=user)(sAMAccountName={0}))";

ds.SearchScope = SearchScope.Subtree;
ds.PropertiesToLoad.Add("distinguishedName");
ds.PageSize = 1;
ds.ServerPageTimeLimit = TimeSpan.FromSeconds(2);
ds.Filter = string.Format(userFilter, userName);

SearchResult sr = ds.FindOne();
// now do something with sr.Properties["distinguishedName"][0]
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top