How to use the objectGUID get a DirectoryEntry?

cciikk picture cciikk · Jul 11, 2011 · Viewed 10.8k times · Source

I know ,we can get a DirectoryEntry like this:

string conPath = "LDAP://10.0.0.6/DC=wds,DC=gaga,DC=com";
string conUser = "administrator";
string conPwd = "Iampassword";
DirectoryEntry de = new DirectoryEntry(conPath, conUser, conPwd, AuthenticationTypes.Secure);

and we can change a user's password like this:

DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot = de;
deSearch.Filter = String.Format("sAMAccountName={0}", "xumai");
SearchResultCollection results = deSearch.FindAll();
foreach (SearchResult objResult in results)
{
    DirectoryEntry obj = objResult.GetDirectoryEntry();
    obj.Invoke("setPassword", new object[] { "Welcome99" });
    obj.CommitChanges();
}

if use

string x = obj.Guid.ToString();;

we can get the user's objectGUID "0b118130-2a6f-48d0-9b66-c12a0c71d892"

how can i change it is password base this objectGUID ?

how to search the user base this objectGUID form "LDAP://10.0.0.6/DC=wds,DC=gaga,DC=com"?

is there any way filter it ? etc strFilter = "(&(objectGUID=0b118130-2a6f-48d0-9b66-c12a0c71d892))";

hope for your help

thanks.

Answer

JPBlanc picture JPBlanc · Jul 12, 2011

Without changing you code you've got multiple way to bind to Active-Directory. Here are two others ways :

The first one use GUID to bind to an object:

string conPath = "LDAP://10.0.0.6/<GUID=0b118130-2a6f-48d0-9b66-c12a0c71d892>";

The second one use SID to bind to an object:

string conPath = "LDAP://10.0.0.6/<SID=S-X-X-XX-XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXX-XXX>"; 

Using security Principals you can do it like that :

UserPrincipal user = UserPrincipal.FindByIdentity(adPrincipalContext, IdentityType.DistinguishedName,"CN=User1Acct,OU=TechWriters,DC=wds,DC=gaga,DC=com");

or

UserPrincipal user = UserPrincipal.FindByIdentity(adPrincipalContext, IdentityType.Guid,"0b118130-2a6f-48d0-9b66-c12a0c71d892");