Question

I have GroupPrincipal extension that adds several extensionAttributes:

[DirectoryObjectClass("group")]
[DirectoryRdnPrefix("CN")]

public class GroupPrincipalsEx : GroupPrincipal
{
    public GroupPrincipalsEx(PrincipalContext context) : base(context) { }

    public GroupPrincipalsEx(PrincipalContext context, string samAccountName)
        : base(context, samAccountName)
    {
    }

    [DirectoryProperty("ExtensionAttribute1")]
    public string ExtensionAttribute1
    {
        get
        {
            if (ExtensionGet("ExtensionAttribute1").Length != 1)
                return null;

            return (string)ExtensionGet("ExtensionAttribute1")[0];

        }
        set { this.ExtensionSet("ExtensionAttribute1", value); }
    }

    [DirectoryProperty("ExtensionAttribute2")]
    public string ExtensionAttribute2
    {
        get
        {
            if (ExtensionGet("ExtensionAttribute2").Length != 1)
                return null;

            return (string)ExtensionGet("ExtensionAttribute2")[0];

        }
        set { this.ExtensionSet("ExtensionAttribute2", value); }
    }

    [DirectoryProperty("ExtensionAttribute3")]
    public string ExtensionAttribute3
    {
        get
        {
            if (ExtensionGet("ExtensionAttribute3").Length != 1)
                return null;

            return (string)ExtensionGet("ExtensionAttribute3")[0];

        }
        set { this.ExtensionSet("ExtensionAttribute3", value); }
    }

    [DirectoryProperty("ExtensionAttribute4")]
    public string ExtensionAttribute14
    {
        get
        {
            if (ExtensionGet("ExtensionAttribute4").Length != 1)
                return null;

            return (string)ExtensionGet("ExtensionAttribute4")[0];

        }
        set { this.ExtensionSet("ExtensionAttribute4", value); }
    }
}

I need to get list of groups that specific user is member of and extensionAttribute1 of that group is specific value. It should return array of custom objects (group SamAccountName, DN, extensionAttributes 1-4).

I know how to get all groups user is member, but I'm having problem to cast GroupPrincipal to GroupPrincipalsEx.

public List<GroupPrincipalsEx> GetGroups(string userName, Boolean Recurent = false)
    {
        List<GroupPrincipalsEx> result = new List<GroupPrincipalsEx>();

        // establish domain context
        PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain);

        // find your user
        UserPrincipal user = UserPrincipal.FindByIdentity(yourDomain, userName);

        // if found - grab its groups
        if (user != null)
        {
            PrincipalSearchResult<Principal> groups = Recurent ? user.GetAuthorizationGroups() : user.GetGroups();

            // iterate over all groups
            foreach (Principal p in groups)
            {
                // make sure to add only group principals
                if (p is GroupPrincipal)
                {
                    //I need a way to cast p from GroupPrincipal to GroupPrincipalsEx and then add it to array
                    GroupPrincipalsEx gp = (GroupPrincipalsEx)p;
                    if(gp.ExtensionAttribute1 == "Specific Value")
                       result.Add(gp);
                }
            }
        }

        return result;
    }

Any idea how to do this without using DirectoryEntry?

No correct solution

OTHER TIPS

You should reconsider your class hierarchy. Casting to child class is never the best practice. Anyway I would code it like this:

var gp = p as GroupPrincipalsEx;
// make sure to add only group principals
if (gp != null && gp.ExtensionAttribute1 == "Specific Value")
   result.Add(gp)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top