我有以下C#代码,它应该将用户添加到现有组。现在,每次我尝试将用户添加到组时,都会引发以下错误:
无法将“System .__ ComObject”类型的COM对象强制转换为接口类型“IADsGroup”。此操作失败,因为对IID为“{27636B00-410F-11CF-B1FF-02608C9E7553}”的接口的COM组件的QueryInterface调用由于以下错误而失败:不支持此类接口(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)
您是否可以尝试稍微重构代码以同时使用GroupPrincipal
和UserPrincipal
:
try {
GroupPrincipal group = GroupPrincipal.Find....
UserPrincipal user = UserPrincipal.Find....
group.Members.Add( user );
}
这是否也会引发异常?