Your help is very much appreciated.
I am trying to get the groups a specific user belongs to from Active Directory.
Iv'e see many examples of how to do it.
But, I don't understand one basic thing...
How to tell Active Directory, which user I want to get the groups for????
in the examples I see connections string like:
"LDAP://DC=Microsoft,DC=COM" "name" "Microsoft"
What is DC??????
My connection string:
DirectoryEntry x = new DirectoryEntry(LDAP://myservername,"username","password",System.DirectoryServices.AuthenticationTypes.ServerBind);
will get me connected to Active directory.but how do I specify which user I want to enquire????
the user name and password used in the code above are for connecting to active directory...
Thanks
Roy
Klaus 0Posted Jun 11, 2007, 12:00 PM
string user = "mike"; //loging name to search for...
DirectoryEntry adEntry = new DirectoryEntry(connectionString);
DirectorySearcher adSearch = new DirectorySearcher(adEntry);
adSearch.Filter = "(cn=" + user + ")";
//this is basicly your querystring...
adSearch.PropertiesToLoad.Add("cn");
adSearch.PropertiesToLoad.Add("mail");
SearchResult result = adSearch.findOne();
result.Properties["cn"][0].toString();
//Returns individuel records on the profile - in this case "cn" - Im not that sure on this command tho, but some thing like that :)
If you search for several users or in other ways would get more than one result back use:
SearchResultCollection instead of SearchResult
adSearch.FindAll() instead of adSearch.FindOne();
For the Groups...
They are usually(if not always?) stored in a field called "ou"(Organization Units). Pull that data like and other fields - just remember that u get a array back with the data...
adSearch.PropertiesToLoad.add("ou");
.....
adSearch.Properties["ou"][0];
adSearch.Properties["ou"][1];
adSearch.Properties["ou"][2];
All above code is just written from memory so dont exspect to be able to copy/paste it :)
Good luck!