【问题标题】:Having problems adding users to an Active Directory group using C#使用 C# 将用户添加到 Active Directory 组时遇到问题
【发布时间】:2013-02-06 23:13:46
【问题描述】:

好的,我现在的问题是我们正在尝试编写将用户添加到我们 Active Directory 中不同组的代码。这是我们编写的解决方案。

部分main方法:

string newGroup = "TestDelete";
string userName = result.Properties["cn"][0].ToString();
string adduser = ad.AddToGroup(userName, newGroup);
Console.WriteLine(String.Format("{0} : {1}",userName, adduser)); 

从另一个类调用这个方法:

public String AddToGroup(string userDn, string groupDn)
{
    try
    {
        DirectoryEntry ldapConnection = new DirectoryEntry("LDAP://OU=" + groupDn + ",DC=blah,DC=blah,DC=blah");
        ldapConnection.AuthenticationType = AuthenticationTypes.Secure;

        string newUser = "CN=" + userDn + "CN=Members,DC=blah,DC=blah,DC=blah";

        ldapConnection.Invoke("Add", new object[] { newUser });
        ldapConnection.CommitChanges();
        ldapConnection.Close();

        return "Success";
    }
    catch (System.DirectoryServices.DirectoryServicesCOMException E)
    {
        Console.WriteLine("Exception caught:\n\n" + E.ToString());
    }
}

抛出异常

System.Runtime.InteropServices.COMException (0x80020006):未知名称。 (来自 HRESULT 的异常:0x80020006 (DISP_E_UNKNOWNNAME))
在 System.DirectoryServices.DirectoryEntry.InvokeSet(String propertyName, Object[] args)
在 C:\Users\XXX\Documents\Visual Studio 2010\Projects\UserPruning\adjustUsers\Program.cs:line 45 中的 adjustUsers.Program.AddToGroup(String userDn, String groupDn)
在 C:\Users\XXX\Documents\Visual Studio 2010\Projects\UserPruning\UserPruning\MainProgram.cs: 46 行中的 UserPruning.MainProgram.Main(String[] args) 处

据我们所知,这表明我们的语法存在问题。

第 46 行是

string adduser = ad.AddToGroup(userName,newGroup)

第 45 行是

ldapConnection.Invoke("Add", new object[] {newUser});

我们在最后一天一直在尝试重写这段代码,但仍然感到困惑。

帮助?

谢谢

【问题讨论】:

    标签: c# active-directory windows-server-2008-r2 directoryservices active-directory-group


    【解决方案1】:

    如果您使用的是 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:

    基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

    // set up domain context
    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
    {
        // find your user
        UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
    
        if(user != null)
        {
            // find the group in question
            GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "TestDelete");
    
            // if found....
            if (group != null)
            {
                // add user to group
                group.Members.Add(user);
                group.Save();
            }
        }
    }
    

    新的 S.DS.AM 让在 AD 中与用户和组一起玩变得非常容易!

    【讨论】:

    • 我希望我能多次投票赞成这个答案。我觉得我在网上发现的几乎所有关于通过 C# 操作 AD 的内容都链接到 Howto:(几乎)通过 C# [codeproject.com/Articles/18102/… 文章在 Active Directory 中的所有内容,该文章使用了 DirectoryEntry API。 DirectoryServices API 要好得多!非常感谢!
    • 问题是 AccountManagement 命名空间只有在运行它的计算机在该域上并且可以解析您正在查询的域时才有效。
    • 我正在查询另一个域,这可能是出现异常“无法检索有关域的信息 (1355)”的原因。 ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多