我使用下面的代码从 Active Directory 获取用户的安全组列表,但是我从 Active Directory 获取用户的所有组,你能帮我从 Active Directory 获取用户的安全组列表吗?
public List<string> getSecurityGroup(string userName,SearchInfo searchInfo, Error errorInfo)
{
List<string> groups =new List<string>() ;
try
{
string ldapPath = @"LDAP://" + searchInfo.HostName + @"/" + searchInfo.SearchDN;
DirectoryEntry obEntry = new DirectoryEntry(ldapPath);
DirectorySearcher srch = new DirectorySearcher(obEntry,
"(sAMAccountName=" + userName + ")");
SearchResult res = srch.FindOne();
if (null != res)
{
DirectoryEntry obUser = new DirectoryEntry(res.Path);
// Invoke Groups method.
object obGroups = obUser.Invoke("Groups");
foreach (object ob in (IEnumerable)obGroups)
{
// Create object for each group.
DirectoryEntry obGpEntry = new DirectoryEntry(ob);
groups.Add(obGpEntry.Name);
}
}
}
catch (System.Runtime.InteropServices.ExternalException comException)
{
errorInfo.ErrorCode = comException.ErrorCode;
errorInfo.ErrorMessage = comException.Message;
}
catch (Exception exception)
{
errorInfo.ErrorCode = -1;
errorInfo.ErrorMessage = exception.Message;
}
return groups;
}