简体   繁体   中英

Getting a list of AD Groups a user is in

I have this;

PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, httpContext.User.Identity.Name);
PrincipalSearchResult<Principal> authgroups = user.GetAuthorizationGroups();
PrincipalSearchResult<Principal> userGroups = user.GetGroups();

And it works to a point. However the groups I am getting back don't really look like what I was expecting.

Using gpresult /V from the command prompt gives me a list that looks something like;

    BUILTIN\Administrators
    Everyone
    SQLServerMSSQLServerADHelperUser$ITVN1259
    BUILTIN\Users

But using the code gives me;

Name ( "zz.Enterprise Vault Users Group 3" )
Name ( "CM-InternetAccessUsers(C)-SZ" )

How do I get the same list as gpresult does?

I use something like this - I think you're only missing the SamAccountName part:

    public string[] Groups
    {
        get { return UserPrincipal.Current.GetAuthorizationGroups()
                                          .Select(e => e.SamAccountName)
                                          .ToArray(); }
    }

EDIT : Use GetAuthorizationGroups() instead of GetGroups() for a recursive search (only returns security groups, not distribution groups).

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