简体   繁体   中英

How to get AD groups a user is in specifying username and domain

I have a function that returns a list of AD groups a user is in.

    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;
            }

        }
    }

This is working as I would expect. I would like to update this function so I can pass it an LDAP path to specify the domain I want to query.

I have searched for hours and can find any pointers (even though I am sure the answer is out there somewhere!) I would really appreciate any help here.

您可以仅添加一个新参数,例如string domainName ,并将其传递给new PrincipalContext()而不是Environment.UserDomainName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM