[英]Get a list of members of a WinNT group
堆棧溢出有幾個與此類似的問題,但不完全相同。
我想在win xp計算機上打開或創建一個本地組,並向其添加成員,域名,本地和眾所周知的帳戶。 我還想檢查一個用戶是否已經是一個成員,這樣我就不會兩次添加同一個帳戶,並且可能會出現異常。
到目前為止,我開始將DirectoryEntry對象與WinNT://
提供程序一起使用。 這樣可以,但是我仍然堅持如何獲得一個組成員列表?
有人知道怎么做嗎? 或者提供比使用DirectoryEntry更好的解決方案?
好的,花了一段時間,搞亂了不同的解決方案,但最符合我原始問題的解決方案如下。 我無法使用“標准”方法獲取DirectoryEntry對象來訪問本地組的成員,我可以通過使用Invoke方法調用本機對象Members方法來枚舉成員的唯一方法。
using(DirectoryEntry groupEntry = new DirectoryEntry("WinNT://./Administrators,group")) { foreach(object member in (IEnumerable) groupEntry.Invoke("Members")) { using(DirectoryEntry memberEntry = new DirectoryEntry(member)) { Console.WriteLine(memberEntry.Path); } } }
我還使用了類似的技術來添加和刪除本地組中的成員。
希望這對其他人也有幫助。 基思。
編輯 Tim:添加了VB.Net版本
Public Function MembersOfGroup(ByVal GroupName As String) As List(Of DirectoryEntry)
Dim members As New List(Of DirectoryEntry)
Try
Using search As New DirectoryEntry("WinNT://./" & GroupName & ",group")
For Each member As Object In DirectCast(search.Invoke("Members"), IEnumerable)
Dim memberEntry As New DirectoryEntry(member)
members.Add(memberEntry)
Next
End Using
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
Return members
End Function
Microsoft .NET Framework提供了一個標准庫,用於在System.DirectoryServices.dll中使用Active Directory: System.DirectoryServices命名空間 。
Microsoft建議使用System.DirectoryServices命名空間中的兩個主要類: DirectoryEntry和DirectorySearcher 。 在大多數情況下,僅使用DirectorySearcher類就足夠了。
更新:我在我的機器上測試它 - 它的工作原理。 但也許我誤解了你的問題。
以下是一篇優秀的CodeProject文章的示例:
using System.DirectoryServices;
ArrayList GetADGroupUsers(string groupName)
{
SearchResult result;
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(cn={0})", groupName);
search.PropertiesToLoad.Add("member");
result = search.FindOne();
ArrayList userNames = new ArrayList();
if (result != null)
{
for (int counter = 0; counter <
result.Properties["member"].Count; counter++)
{
string user = (string)result.Properties["member"][counter];
userNames.Add(user);
}
}
return userNames;
}
您應該能夠在DirectoryEntry
上代表該組的"member"
屬性中找到此信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.