我想找到用户所属的网上论坛列表。我尝试了几种解决方案
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;
}
谢谢,
更新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
类以以下方式非常轻松地执行此操作:
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
的引用以引入所需的类型。