从Windows服务获取用户所属组的列表
问题描述:
从Windows服务中检索用户所属组的列表的最佳方式是什么?从Windows服务获取用户所属组的列表
List<string> groups = new List<string>();
foreach (IdentityReference ir in new WindowsIdentity(name).Groups)
{
SecurityIdentifier sid = new SecurityIdentifier(ir.Value);
NTAccount ntAccount = (NTAccount)sid.Translate(typeof(NTAccount));
groups.Add(ntAccount.ToString());
}
我试图使用上面的代码,但它引发了以下错误。
Error communicating with client: System.Security.SecurityException: Incorrect function.
答
下面是我最后使用的代码。我不知道LDAP,但它似乎可能引起一些安全问题...
public static List<string> GetUserGroups(string name)
{
List<string> groups = new List<string>();
DirectorySearcher search = new DirectorySearcher("");
int groupCount;
int counter;
string GroupName;
string DataToWriteGroups;
search.Filter = "(&(objectClass=user)(SAMAccountName=" + name + "))";
search.PropertiesToLoad.Add("memberOf");
SearchResult result = search.FindOne();
groupCount = result.Properties["memberOf"].Count;
if (groupCount > 0)
{
DataToWriteGroups = "Group(s) Belongs To User - " + name + "";
for (counter = 0; counter <= groupCount - 1; counter++)
{
GroupName = "";
GroupName = (result.Properties["memberOf"][counter].ToString());
groups.Add(GroupName);
}
}
return groups;
}
我不知道这是如何帮助我的情况。 – Anatoli 2010-07-23 18:36:51
我想通了...谢谢... – Anatoli 2010-07-23 19:59:23