问题
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.
回答1:
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");
回答2:
If .NET 3.5 is an option, you should start using System.DirectoryServices.AccountManagement
. It is a whole new world. Here's is the code to find a user by the GUID:
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain,
"LDAP://10.0.0.6",
"DC=wds,DC=gaga,DC=com",
"administrator",
"Iampassword"))
{
string theGuid = "0b118130-2a6f-48d0-9b66-c12a0c71d892";
UserPrincipal up = UserPrincipal.FindByIdentity(pc, IdentityType.Guid, theGuid);
}
The same template would be easily adapted to other object types.
来源:https://stackoverflow.com/questions/6651586/how-to-use-the-objectguid-get-a-directoryentry