3
votes

I have the following C# Code which should add a user to a existing group. Now every time I try to add a user to a group the following error is thrown:

Unable to cast COM object of type 'System.__ComObject' to interface type 'IADsGroup'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{27636B00-410F-11CF-B1FF-02608C9E7553}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).System.DirectoryServices.AccountManagement

Below is the relevant code:

// Clearing result message variable before using
    sResult = "";

    bool bGroupMemberOf = false;
    using (PrincipalContext sourceContext = new PrincipalContext(ContextType.Domain, sDomainName))
    {
        try
        {
            GroupPrincipal group = GroupPrincipal.FindByIdentity(sourceContext, IdentityType.Name, sGroupName);
            if (group.Members.Contains(sourceContext, IdentityType.SamAccountName, sAccountName))
            {
                sResult += sAccountName + " already member of" + group.Name + Environment.NewLine;
            }
            group.Members.Add(sourceContext, IdentityType.SamAccountName, sAccountName);
            group.Save();
            group.Dispose();

            sResult += sAccountName + " is now member of " + group.Name + Environment.NewLine;
        }
        catch (Exception error)
        {
            return  error.Message + "-" + error.Source + Environment.NewLine;
        }
    }
    return sResult;

Can somebody tel me what is going wrong here. I can hardly find any reference to the error I receive.

1
Which line throws the exception?Wiktor Zychla
group.members.add(....) throws the exeception.Jangac

1 Answers

1
votes

Could you try to refactor the code slightly to use both GroupPrincipal and UserPrincipal, like this:

try {
   GroupPrincipal group = GroupPrincipal.Find....
   UserPrincipal user = UserPrincipal.Find....

   group.Members.Add( user );
}

Does this also throw an exception?