GroupPrincipal.Members.Add(userPrincipal) 引发错误
我有以下 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;
可以有人告诉我这里出了什么问题。我几乎找不到任何关于我收到的错误的参考。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能否尝试稍微重构代码以同时使用
GroupPrincipal
和UserPrincipal
,如下所示:这也会引发异常吗?
Could you try to refactor the code slightly to use both
GroupPrincipal
andUserPrincipal
, like this:Does this also throw an exception?