为特定用户的用户组查询LDAP
问题描述:
我必须在C#中为特定用户检查LDAP Active Directory的用户组。意思是我将这个用户名传递给一个方法,它返回该用户所属的组的列表。你能帮我吗?我搜索很多但每次都会得到新的错误。为特定用户的用户组查询LDAP
LDAP路径:192.168.1.4
域名:阿尔斯兰
用户名:ArslanP
密码:testad
答
既然你在.NET 3.5及以上,你应该看看System.DirectoryServices.AccountManagement
(S.DS.AM)命名空间。在这里阅读全部内容:
Managing Directory Security Principals in the .NET Framework 3.5
基本上,引用添加到组装System.DirectoryServices.AccountManagement
,然后你可以定义域范围内,并可以轻松地查找用户和/或组AD:
using System.DirectoryServices.AccountManagement;
public List<GroupPrincipal> GetGroupsForUser(string username)
{
List<GroupPrincipal> result = new List<GroupPrincipal>();
// set up domain context - if you do a lot of requests, you might
// want to create that outside the method and pass it in as a parameter
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find user by name
UserPrincipal user = UserPrincipal.FindByIdentity(username);
// get the user's groups
if(user != null)
{
foreach(GroupPrincipal gp in user.GetAuthorizationGroups())
{
result.Add(gp);
}
}
return result;
}
新的S.DS.AM使它可以很容易地与AD中的用户和群组一起玩:
答
此相关的问题可以帮助你:
Get List of Users From Active Directory In A Given AD Group
它要求th一个反向问题,当你知道该组时,如何获得用户列表,但其他答案也可能对你有用。
又见这个问题的答案:
发布代码给你带来问题,也许我们可以帮助它。 – SGarratt 2011-03-19 16:17:46