如何从参数中获取所有组?方法GetGroups返回Memberof +主组。

public static ArrayList GetUserGroups(string sUserName)
{
    ArrayList myItems = new ArrayList();
    UserPrincipal oUserPrincipal = GetUser(sUserName);

    PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();

    foreach (Principal oResult in oPrincipalSearchResult)
    {
        if(NOT_PRIMARY) // <-- check by primary group ID? 
                        // How get group GUID/ CID by ID ?
            myItems.Add(oResult.Name);
    }
    return myItems;
}
.

有帮助吗?

解决方案

有一个解决方案:

    public static ArrayList GetUserGroupsExcludingPrimaryGroup(string sUserName)
    {
        ArrayList myItems = new ArrayList();
        UserPrincipalEx oUserPrincipal = GetUser(sUserName);

        var objectSid = oUserPrincipal.ObjectSid.ToString();
        objectSid = objectSid.Substring(0, objectSid.LastIndexOf("-")) + "-" + oUserPrincipal.PrimaryGroupID;

        PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();

        foreach (Principal oResult in oPrincipalSearchResult)
        {
            if (objectSid != oResult.Sid.ToString())
                myItems.Add(oResult.Name);
        }
        return myItems;
    }
.

accountmanagement - 覆盖

namespace System.DirectoryServices.AccountManagement
{
    [DirectoryRdnPrefix("CN")]
    [DirectoryObjectClass("User")]

    public class UserPrincipalEx : UserPrincipal
    {
        public UserPrincipalEx(PrincipalContext context) : base(context) { }
        public UserPrincipalEx(PrincipalContext context, string samAccountName, string password, bool enabled) : base(context, samAccountName, password, enabled) { }

        ...

        [DirectoryProperty("primaryGroupID")]
        public Int32 PrimaryGroupID
        {
            get { return (Int32)ExtensionGet("primaryGroupID")[0]; }
            set { ExtensionSet("primaryGroupID", value); }
        }

        [DirectoryProperty("objectSid")]
        public SecurityIdentifier ObjectSid
        {
            get { return new SecurityIdentifier((byte[])ExtensionGet("objectSid")[0], 0); }
            set { ExtensionSet("objectSid", value); }
        }

    }
}
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top