GroupPrincipal.Members.Add(userPrincipal) 引发错误

发布于 2024-12-18 21:39:08 字数 1389 浏览 1 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沧笙踏歌 2024-12-25 21:39:08

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

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

   group.Members.Add( user );
}

这也会引发异常吗?

Could you try to refactor the code slightly to use both GroupPrincipal and UserPrincipal, like this:

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

   group.Members.Add( user );
}

Does this also throw an exception?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文