C#Active Directory添加用户

时间:2017-10-23 12:59:40

标签: c# active-directory organizational-unit

我试图创建一个必须填写的文本框的用户。属性,用户名和密码是正确的但我得到了一个"服务器上没有这样的对象"错误。

private void btn_AddStudent_Click(object sender, EventArgs e)
    {
        try
        {
            // Username and password.
            string UserName = UsernameGenerate(8);
            string Password = PasswordGenerate(8);

            // OU path.
            string ouString = "OU = " + cmb_Study.Text;
            string LDAPstring = "LDAP://" + "OU = Studies, " + ouString + ", DC=DR, DC=GUI";
            DirectoryEntry dirEntry = new DirectoryEntry(LDAPstring);

            // Create user.
            string userString = "CN = " + UserName;
            DirectoryEntry newUser = dirEntry.Children.Add(userString, "user");
            newUser.CommitChanges();

            newUser.Properties["userprincipalname"].Add(UserName + "@DR.GUI");
            newUser.Properties["samaccountname"].Value = UserName;
            newUser.Invoke("setPassword", new object[] {Password});
            //Rest of the properties

            newUser.Close();
            dirEntry.Close();
            newUser.Dispose();
            dirEntry.Dispose();
            MessageBox.Show("User has been succesfully added");
        }
        catch (Exception E)
        {
            MessageBox.Show("Creating user has failed. " + E.Message);
        }
    }

我一直试图寻找解决方案已经有一段时间了,但我仍然无法解决问题所在。我在OU路径上犯了错误吗?例如,OU = Studies / ISM或Studies / WEBDEV。或者我在LDAP中犯了什么错误?

我是C#的新手并且总体编程,如果可能的话,我正在寻找一个简单的解决方案。

1 个答案:

答案 0 :(得分:2)

我发现管理这个问题的方法不那么痛苦是PrincipalContext

像PInvoke或DirectoryServices这样的其他方式给我带来了某种方式的头痛

如果您拥有这些权利,它可以完美地运作:

try
{
   using (var pcLocal = new PrincipalContext(ContextType.Machine))
   {
      var group = GroupPrincipal.FindByIdentity(pcLocal, "Administrators");

      using (var pcDomain = new PrincipalContext(ContextType.Domain, "AAA"))
      {
         group.Members.Add(pcDomain, IdentityType.SamAccountName, "User123");                            group.Save();
      };
   };
} catch (Exception e)
{
   Console.WriteLine(e.Message);
};

或者:

using(PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "Fabrikam", "ou=TechWriters,dc=fabrikam,dc=com"))
{

 UserPrincipal user = new UserPrincipal(ctx, userName, userPassword, true);

    // assign some properties to the user principal
    user.GivenName = "User";
    user.Surname = "One";

    // force the user to change password at next logon
    user.ExpirePasswordNow();

    // save the user to the directory
    user.Save();
}

用于在子尝试中创建用户:

//ADPath1 = LDAP://x.x.x./OU=OUStudents,DC=mydomain,DC=local
//ADUser = AD Admin User
//ADPassword = AD Admin user's password
 DirectoryEntry de = new DirectoryEntry(ADPath1, ADUser, ADPassword, AuthenticationTypes.Secure);
            // 1. Create user account
            DirectoryEntries users = de.Children;
            DirectoryEntry newuser = users.Add("CN=" + userid, "user");
           //2. Set properties
//....

newuser.CommitChanges();