2011-07-11 24 views
2

我知道,我們可以得到這樣一個DirectoryEntry:如何使用objectGUID獲取DirectoryEntry?

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); 

,我們可以這樣修改用戶的密碼:

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(); 
} 

如果使用

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

我們可以得到user's objectGUID「0b118130-2a6f-48d0-9b66-c12a0c71d892」

怎麼可以我改變它是基於這個objectGUID的密碼?

如何搜索這個objectGUID形式的用戶羣「LDAP://10.0.0.6/DC=wds,DC=gaga,DC=com」?

有什麼方法可以過濾嗎? etc「strFilter =」(&(objectGUID = 0b118130-2a6f-48d0-9b66-c12a0c71d892))「;

希望對您有所幫助

謝謝。

回答

6

沒有改變你的代碼你有multiple way to bind to Active-Directory。這裏有兩個其他方式:

第一個use GUID to bind to an object

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

第二個use SID to bind to an object

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

使用security Principals你可以做這樣的:

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

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

它很好用,謝謝! – cciikk

0

如果.NET 3.5是一個選項,您應該開始使用System.DirectoryServices.AccountManagement。這是一個全新的世界。以下是通過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); 
} 

相同的模板很容易適應其他對象類型。

+0

噢謝謝你,但我需要將「LDAP://10.0.0.6/」更改爲「10.0.0.6」,它運行良好。 以及如果GUID是一個OU,如何做到這一點? 我使用了System.DirectoryServices.AccountManagement命名空間,並且沒有一個OU類。 – cciikk

+0

糟糕,複製並粘貼錯字。修正了。我會盡快發佈OU的更新 –