如何获取用户的AD组是指定用户名和域

时间:2013-03-06 14:24:48

标签: c# asp.net active-directory

我有一个函数可以返回用户所在的AD组列表。

    public static List<string> GetGroupNames(string userName)
    {

        using (var context = new PrincipalContext(ContextType.Domain, Environment.UserDomainName))
        {
            using (var userPrincipal = UserPrincipal.FindByIdentity(context, userName))
            {
                var groupSearch = userPrincipal.GetGroups(context);
                var result = new List<string>();
                groupSearch.ToList().ForEach(sr => result.Add(sr.SamAccountName));

                return result;
            }

        }
    }

这正如我所料。我想更新此函数,以便我可以传递一个LDAP路径来指定我想要查询的域。

我已经搜索了几个小时,可以找到任何指针(尽管我确定答案已经出现在某处!)我真的很感激这里有任何帮助。

1 个答案:

答案 0 :(得分:1)

你可以添加一个新参数,比方说string domainName并将其传递给new PrincipalContext()而不是Environment.UserDomainName