2011-11-30 38 views
2

我有下面的C#代码,它应该将用户添加到现有组。现在每次我尝试将用户添加到组下面的错误被抛出:GroupPrincipal.Members.Add(userPrincipal)引发错误

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

下面是相关代码:

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

有人可以电话我是怎么回事错在这里。我几乎找不到我收到的错误。

+0

哪一行引发异常? –

+0

group.members.add(....)会引发怀疑。 – Jangac

回答

0

你能尝试稍微重构代码同时使用GroupPrincipalUserPrincipal,像这样:

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

    group.Members.Add(user); 
} 

这是否也抛出一个异常?

+1

我将您示例中的代码重构为我提供的代码。该应用程序是Windows 2008 R2服务器上的网站。错误消息中的GUID属于ActiveDS.dll – Jangac