我想找到用户所属的组列表。我尝试了几种解决方案http://www.codeproject.com/KB/system/everythingInAD.aspx http://www.codeproject.com/KB/system/everythingInAD.aspx但没有结果。
此代码给我一个“true”,意味着 LDAP 正在运行:
public static bool Exists(string objectPath)
{
bool found = false;
if (DirectoryEntry.Exists("LDAP://" + objectPath))
found = true;
return found;
}
Thanks,
更新1:
public ArrayList Groups(string userDn, bool recursive)
{
ArrayList groupMemberships = new ArrayList();
return AttributeValuesMultiString("memberOf", "LDAP-Server",
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;
}
我有一个例外:
PropertyValueCollection ValueCollection = ent.Properties[attributeName];
“COMException 未处理”
在 .NET 4 中,您可以使用新的UserPrincipal http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal.aspx通过以下方式进行类:
using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "your_login");
foreach (var group in user.GetGroups())
{
Console.WriteLine(group.Name);
}
}
您必须添加对System.DirectoryServices.AccountManagement
引入所需的类型。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)