当我检索本地 WinNT 组的成员时,不知何故并非所有成员都返回。我确实补充说:
- 活动目录用户
- 活动目录组
两者都成功(见图),但之后只有用户出现。
问题是:
- 添加的组会怎样?
- 请参阅代码示例“GetMembers()”中的最后一个方法
- 这是一个已知的问题?
- 有什么可用的解决方法吗?
非常感谢!!
string _domainName = @"MYDOMAIN";
string _basePath = @"WinNT://MYDOMAIN/myserver";
string _userName = @"MYDOMAIN\SvcAccount";
string _password = @"********";
void Main()
{
CreateGroup("lg_TestGroup");
AddMember("lg_TestGroup", @"m.y.username");
AddMember("lg_TestGroup", @"Test_DomainGroup");
GetMembers("lg_TestGroup");
}
// Method added for reference.
void CreateGroup(string accountName)
{
using (DirectoryEntry rootEntry = new DirectoryEntry(_basePath, _userName, _password))
{
DirectoryEntry newEntry = rootEntry.Children.Add(accountName, "group");
newEntry.CommitChanges();
}
}
// Add Active Directory member to the local group.
void AddMember(string groupAccountName, string userName)
{
string path = string.Format(@"{0}/{1}", _basePath, groupAccountName);
using (DirectoryEntry entry = new DirectoryEntry(path, _userName, _password))
{
userName = string.Format("WinNT://{0}/{1}", _domainName, userName);
entry.Invoke("Add", new object[] { userName });
entry.CommitChanges();
}
}
// Get all members of the local group.
void GetMembers(string groupAccountName)
{
string path = string.Format(@"{0}/{1}", _basePath, groupAccountName);
using (DirectoryEntry entry = new DirectoryEntry(path, _userName, _password))
{
foreach (object member in (IEnumerable) entry.Invoke("Members"))
{
using (DirectoryEntry memberEntry = new DirectoryEntry(member))
{
string accountName = memberEntry.Path.Replace(string.Format("WinNT://{0}/", _domainName), string.Format(@"{0}\", _domainName));
Console.WriteLine("- " + accountName); // No groups displayed...
}
}
}
}
更新#1 组成员的顺序似乎是必不可少的。一旦GetMembers()中的枚举器偶然发现Active Directory 组,其余项目也不会显示。因此,如果在此示例中首先列出了“Test_DomainGroup”,则GetMembers()根本不会显示任何内容。