有使用上一個DirectoryEntry的「組」屬性的兩個已知問題:
- 它不會告訴你「默認組」用戶是在(通常爲「用戶」)
- 它不會告訴你嵌套組成員
因此,如果用戶是A組的成員,該組隨後又是B組的成員,然後在Windows中,這意味着用戶還成員 B組。但是,DirectoryEntry不會向您顯示嵌套的組成員資格。
這些是我知道的直接Active Directory(無Exchange)的兩個限制。
獲取默認組有點涉及,但我確實有一個代碼示例。
private string GetPrimaryGroup(DirectoryEntry aEntry, DirectoryEntry aDomainEntry)
{
int primaryGroupID = (int)aEntry.Properties["primaryGroupID"].Value;
byte[] objectSid = (byte[])aEntry.Properties["objectSid"].Value;
StringBuilder escapedGroupSid = new StringBuilder();
// Copy over everything but the last four bytes(sub-authority)
// Doing so gives us the RID of the domain
for(uint i = 0; i < objectSid.Length - 4; i++)
{
escapedGroupSid.AppendFormat("\\{0:x2}", objectSid[i]);
}
//Add the primaryGroupID to the escape string to build the SID of the primaryGroup
for(uint i = 0; i < 4; i++)
{
escapedGroupSid.AppendFormat("\\{0:x2}", (primaryGroupID & 0xFF));
primaryGroupID >>= 8;
}
//Search the directory for a group with this SID
DirectorySearcher searcher = new DirectorySearcher();
if(aDomainEntry != null)
{
searcher.SearchRoot = aDomainEntry;
}
searcher.Filter = "(&(objectCategory=Group)(objectSID=" + escapedGroupSid.ToString() + "))";
searcher.PropertiesToLoad.Add("distinguishedName");
return searcher.FindOne().Properties["distinguishedName"][0].ToString();
}
獲取嵌套組還需要幾個步驟,我將不得不尋找一個解決一個,如果是這樣的問題。
馬克
PS:作爲一個方面說明 - 爲什麼地球上,你做一個 「DirectoryEntry.Invoke(」 組 「NULL)」 的口號?爲什麼不只是枚舉多值(包含多個值)並具有組的DN(可分辨名稱)的DirectoryEntry.Properties [「memberOf」]屬性?
foreach(string groupDN in myUser.Properties["memberOf"])
{
string groupName = groupDN;
}
或者如果您使用的是.NET 3.5,則可以使用S.DS.AccountManagement中的新安全主體類。其中一個是「UserPrincipal」,它有一個名爲「GetAuthorizationGroups()」的方法,它可以免費爲您完成所有這些工作 - 基本上是這樣!
看到一個很好的MSDN article,它描述了這些新的.NET 3.5 S.DS功能。
這些是很好的建議,你對MSDN文章是正確的,我應該在開始之前閱讀它。關於爲什麼我使用invoke方法,在這種情況下,我所需要的只是組名,而這看起來像是一種獲取信息的有效方式。我沒有對Active Directory進行過多的編程,所以我仍然非常瞭解繼續進行的最佳方式。從1.1開始,這個領域已經發展了多少.NET是很有趣的! – HeathenWorld 2009-08-13 01:30:43
完全沒有問題 - 只是好奇你是否有使用Invoke()方法的特殊原因。嘿 - 這就是StackOverflow的全部內容 - 當你沒有看到樹木的森林時幫助你:-) – 2009-08-13 05:17:35
對於鏈接的MSDN文章+1,在3.5中更容易 – tbone 2009-09-28 19:38:43