通过C#确定本地组的成员 [英] Determining members of local groups via C#
问题描述
我不知道是否有人知道如何通过C#获取本地组的成员在远程服务器上编程。这会需要管理员权限?如果是的话有没有什么办法来确认当前登录这些群体的用户的成员资格(或没有)?
I wondered whether anybody knows how to obtain membership of local groups on a remote server programmatically via C#. Would this require administrator permissions? And if so is there any way to confirm the currently logged in user's membership (or not) of these groups?
推荐答案
HOWTO :。(几乎)一切在Active Directory中通过C#是非常有益的,也包括如何遍历AD群组中的成员说明
Howto: (Almost) Everything In Active Directory via C# is very helpfull and also includes instructions on how to iterate AD members in a group.
public ArrayList Groups(string userDn, bool recursive)
{
ArrayList groupMemberships = new ArrayList();
return AttributeValuesMultiString("memberOf", userDn,
groupMemberships, recursive);
}
您还需要这种功能:
public ArrayList AttributeValuesMultiString(string attributeName,
string objectDn, ArrayList valuesCollection, bool recursive)
{
DirectoryEntry ent = new DirectoryEntry(objectDn);
PropertyValueCollection ValueCollection = ent.Properties[attributeName];
IEnumerator en = ValueCollection.GetEnumerator();
while (en.MoveNext())
{
if (en.Current != null)
{
if (!valuesCollection.Contains(en.Current.ToString()))
{
valuesCollection.Add(en.Current.ToString());
if (recursive)
{
AttributeValuesMultiString(attributeName, "LDAP://" +
en.Current.ToString(), valuesCollection, true);
}
}
}
}
ent.Close();
ent.Dispose();
return valuesCollection;
}
如果你现在想用这个AD-方法,您可以使用信息在这篇文章中,但它使用非托管的code:
If you do now want to use this AD-method, you could use the info in this article, but it uses unmanaged code:
HTTP://www.$c$cproject.com/KB/ CS / groupandmembers.aspx
这是他们提出的示例应用程序:
The sample application that they made:
这篇关于通过C#确定本地组的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!