tags:

views:

233

answers:

1

I'm looking for a method to obtain the current logged in user's full Active Direcory LDAP path. e.g.

LDAP://CN=john.smith,OU=UK,OU=Sales,DC=Company,DC=local
+3  A: 

Query the LDAP directory (e.g. the AD) with this filter:

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

The DN of the object returned is the thing you are looking for.

Something like this:

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]
Tomalak