C# Active Directory正在创建用户

C# Active Directory正在创建用户,c#,active-directory,C#,Active Directory,我正试图通过C#脚本将用户添加到active directory。我在网上找到了这个脚本(不是我自己做的)。问题是,我在尝试添加用户时出现以下错误: 指定的目录服务属性或值不存在 这是我现在掌握的代码: private void buttonCreateUser_Click(object sender, EventArgs e) { CreateADSUser(textboxUsername.Text, textboxPassword.Text); } public string Cr

我正试图通过C#脚本将用户添加到active directory。我在网上找到了这个脚本(不是我自己做的)。问题是,我在尝试添加用户时出现以下错误:

指定的目录服务属性或值不存在

这是我现在掌握的代码:

private void buttonCreateUser_Click(object sender, EventArgs e)
{
    CreateADSUser(textboxUsername.Text, textboxPassword.Text);
}

public string CreateADSUser(string username, string password)
{
    String RootDSE;
    try
    {
        DirectorySearcher DSESearcher = new DirectorySearcher();
        RootDSE = DSESearcher.SearchRoot.Path;

        RootDSE = RootDSE.Insert(7, "CN=Users,");

        DirectoryEntry myDE = new DirectoryEntry(RootDSE);
        DirectoryEntries myEntries = myDE.Children;

        DirectoryEntry myDirectoryEntry = myEntries.Add("CN=" + username, "user");
        myDirectoryEntry.Properties["userPrincipalName"].Value = username;
        myDirectoryEntry.Properties["name"].Value = username;
        myDirectoryEntry.Properties["Password"].Value = password;
        myDirectoryEntry.Properties["samAccountName"].Value = username;
        myDirectoryEntry.Properties["FullName"].Value = username;
        myDirectoryEntry.Properties["AccountDisabled"].Value = 0;
        myDirectoryEntry.Properties["PasswordRequired"].Value = 1;

        // Permanent Password?
        myDirectoryEntry.Properties["permpass"].Value = 1;
        myDirectoryEntry.CommitChanges();

        DSESearcher.Dispose();
        myDirectoryEntry.Dispose();

        textboxReports.Text = "Worked!";
        return "Worked!";
    }
    catch (Exception ex)
    {
        textboxReports.Text = ex.Message;
        return ex.Message;
    }
}
using (var pc = new PrincipalContext(ContextType.Domain))
            {
                using (var up = new UserPrincipal(pc))
                {
                    up.SamAccountName = textboxUsername.Text; // Username
                    up.EmailAddress = textboxEmail.Text; // Email
                    up.SetPassword(textboxPassword.Text); // Password
                    up.Enabled = true;
                    up.ExpirePasswordNow();
                    up.Save();
                }
            }

没关系,我已经搞定了

这就是它现在的样子:

private void buttonCreateUser_Click(object sender, EventArgs e)
{
    CreateADSUser(textboxUsername.Text, textboxPassword.Text);
}

public string CreateADSUser(string username, string password)
{
    String RootDSE;
    try
    {
        DirectorySearcher DSESearcher = new DirectorySearcher();
        RootDSE = DSESearcher.SearchRoot.Path;

        RootDSE = RootDSE.Insert(7, "CN=Users,");

        DirectoryEntry myDE = new DirectoryEntry(RootDSE);
        DirectoryEntries myEntries = myDE.Children;

        DirectoryEntry myDirectoryEntry = myEntries.Add("CN=" + username, "user");
        myDirectoryEntry.Properties["userPrincipalName"].Value = username;
        myDirectoryEntry.Properties["name"].Value = username;
        myDirectoryEntry.Properties["Password"].Value = password;
        myDirectoryEntry.Properties["samAccountName"].Value = username;
        myDirectoryEntry.Properties["FullName"].Value = username;
        myDirectoryEntry.Properties["AccountDisabled"].Value = 0;
        myDirectoryEntry.Properties["PasswordRequired"].Value = 1;

        // Permanent Password?
        myDirectoryEntry.Properties["permpass"].Value = 1;
        myDirectoryEntry.CommitChanges();

        DSESearcher.Dispose();
        myDirectoryEntry.Dispose();

        textboxReports.Text = "Worked!";
        return "Worked!";
    }
    catch (Exception ex)
    {
        textboxReports.Text = ex.Message;
        return ex.Message;
    }
}
using (var pc = new PrincipalContext(ContextType.Domain))
            {
                using (var up = new UserPrincipal(pc))
                {
                    up.SamAccountName = textboxUsername.Text; // Username
                    up.EmailAddress = textboxEmail.Text; // Email
                    up.SetPassword(textboxPassword.Text); // Password
                    up.Enabled = true;
                    up.ExpirePasswordNow();
                    up.Save();
                }
            }

这里的问题是这些属性实际上都不存在:

myDirectoryEntry.Properties["Password"].Value = password; 
myDirectoryEntry.Properties["FullName"].Value = username; 
myDirectoryEntry.Properties["AccountDisabled"].Value = 0; 
myDirectoryEntry.Properties["PasswordRequired"].Value = 1; 
myDirectoryEntry.Properties["permpass"].Value = 1; 
这不是你写信给的人:

myDirectoryEntry.Properties["name"].Value = username; 
以下是实际属性名称的顺序(从上到下):

  • 密码-
    unicodewd
  • 全名-
    displayName
  • AccountDisabled-
    userAccountControl
  • PasswordRequired-
    userAccountControl
    (实际上您设置了相反的值-仅当不需要密码时)
  • permPass-
    unicodePwd
    (不确定这一个的目标是什么)

由System.DirectoryServices.AccountManagement提供

              PrincipalContext ouContex = new PrincipalContext(ContextType.Domain, "TestDomain.local",           "OU=TestOU,DC=TestDomain,DC=local");

        for (int i = 0; i < 3; i++)
        {
            try
            {
                UserPrincipal up = new UserPrincipal(ouContex);
                up.SamAccountName = "TestUser" + i;
                up.SetPassword("password");
                up.Enabled = true;
                up.ExpirePasswordNow();
                up.Save();
            }
            catch (Exception ex)
            {

            }
        }
PrincipalContext-ouContex=newprincipalcontext(ContextType.Domain,“TestDomain.local”,“OU=TestOU,DC=TestDomain,DC=local”);
对于(int i=0;i<3;i++)
{
尝试
{
UserPrincipal up=新的UserPrincipal(ouContex);
up.SamAccountName=“TestUser”+i;
设置密码(“密码”);
up.Enabled=true;
up.ExpirePasswordNow();
up.Save();
}
捕获(例外情况除外)
{
}
}

按System.DirectoryServices

To use this namespace you need to add reference  System.DirectoryServices.dll 

       DirectoryEntry ouEntry = new DirectoryEntry("LDAP://OU=TestOU,DC=TestDomain,DC=local");

        for (int i = 3; i < 6; i++)
        {
            try
            {
                DirectoryEntry childEntry = ouEntry.Children.Add("CN=TestUser" + i, "user");
                childEntry.CommitChanges();
                ouEntry.CommitChanges();
                childEntry.Invoke("SetPassword", new object[] { "password" });
                childEntry.CommitChanges();
            }
            catch (Exception ex)
            {

            }
        }
要使用此命名空间,您需要添加reference System.DirectoryServices.dll
DirectoryEntry-ouEntry=newdirectoryEntry(“LDAP://OU=TestOU,DC=TestDomain,DC=local”);
对于(int i=3;i<6;i++)
{
尝试
{
DirectoryEntry-childEntry=ouEntry.Children.Add(“CN=TestUser”+i,“user”);
childEntry.CommitChanges();
ouEntry.CommitChanges();
调用(“SetPassword”,新对象[]{“password”});
childEntry.CommitChanges();
}
捕获(例外情况除外)
{
}
}

请不要在标题前加上“C”之类的前缀。这就是标签的作用。不会再发生了,我不擅长制作标题:(.看可能对我有帮助:),谢谢!哦,我不知道,谢谢!顺便说一句,我在哪里可以找到房产的名称?从这里开始-。深入到类(例如用户),然后打开该页面以查看特定类型对象的属性。您也可以从那里查看有关每个属性的信息。