gpt4 book ai didi

c# - GroupPrincipal.Members.Add(userPrincipal) 抛出错误

转载 作者:行者123 更新时间:2023-11-30 18:02:08 25 4
gpt4 key购买 nike

我有以下 C# 代码,可以将用户添加到现有组。现在每次我尝试将用户添加到组时都会抛出以下错误:

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;

有人可以告诉我这里出了什么问题吗?我几乎找不到对我收到的错误的任何引用。

最佳答案

您能否尝试稍微重构代码以同时使用 GroupPrincipalUserPrincipal,如下所示:

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

group.Members.Add( user );
}

这也会抛出异常吗?

关于c# - GroupPrincipal.Members.Add(userPrincipal) 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8325218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com