我有下面的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;
有人可以电话我是怎么回事错在这里。我几乎找不到我收到的错误。
哪一行引发异常? –
group.members.add(....)会引发怀疑。 – Jangac