DirectoryEntry で「Groups」プロパティを使用する場合、次の 2 つの既知の問題があります。
- ユーザーが属する「デフォルトグループ」(通常は「ユーザー」)は表示されません
- ネストされたグループ メンバーシップは表示されません
したがって、ユーザーがグループ A のメンバーであり、そのグループがグループ B のメンバーである場合、Windows では、ユーザーがグループ B のメンバーでもあることを意味します。メンバーシップ。
これらは、直接の Active Directory (Exchange なし) について私が知っている 2 つの唯一の制限です。
既定のグループを取得するのは少し複雑ですが、そのためのコード サンプルがあります。
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("groups", null)」呼び出しを行っているのですか? 多値 (複数の値を含む) で、グループの DN (識別名) を持つ DirectoryEntry.Properties["memberOf"] プロパティを列挙しないのはなぜですか?
foreach(string groupDN in myUser.Properties["memberOf"])
{
string groupName = groupDN;
}
または、.NET 3.5 を使用している場合は、S.DS.AccountManagement の新しいセキュリティ プリンシパル クラスを利用できます。そのうちの 1 つは「UserPrincipal」であり、「GetAuthorizationGroups()」と呼ばれるメソッドがあり、このすべての面倒な作業を基本的に無料で行います。
これらの新しい .NET 3.5 S.DS 機能について説明している優れたMSDN 記事を参照してください。