當我檢索本地WinNT組的成員時,無論如何不是全部成員被返回。我補充一下:如何獲取本地WinNT組的所有成員?
- 的Active Directory用戶
- Active Directory組
既有成功(見圖片),但只的用戶出現之後。
的問題是:
- 會發生什麼事,以增加羣體?
- 查看代碼示例中的最後一個方法'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()根本不顯示任何內容。