c# - Return information about found user Active Directory -
i making active directry managment tool having trouble getting somethings working. while made class want find specific user , return als information(name,fullname,cn,...). can find information when don't know best way return values class.
here code use far:
directorysearcher search = new directorysearcher(ldapconnectie); search.filter = "(cn=" + username + ")"; searchresult result = search.findone(); if (result != null) { list<string> listldapfields = new list<string>(); list<object> listldapvalues = new list<object>(); resultpropertycollection fields = result.properties; foreach (string ldapfield in fields.propertynames) { listldapfields.add(ldapfield); foreach (object mycollection in fields[ldapfield]) { listldapvalues.add(mycollection); } } }
the program add list. if return cannot search list on "cn" or "name". can find information on index number.
hope can me out.
if you're on .net 3.5 , up, should check out system.directoryservices.accountmanagement
(s.ds.am) namespace. read here:
- managing directory security principals in .net framework 3.5
- msdn docs on system.directoryservices.accountmanagement
basically, can define domain context , find users and/or groups in ad:
// set domain context using (principalcontext ctx = new principalcontext(contexttype.domain)) { // find user userprincipal user = userprincipal.findbyidentity(ctx, username); if(user != null) { // here.... // used attributes available nice, strongly-typed properties string value = user.givenname; value = user.surname; value = user.emailaddress; value = user.voicetelephonenumber; } }
the new s.ds.am makes easy play around users , groups in ad!
Comments
Post a Comment