C#Active Directory设置用户属性

时间:2017-10-27 08:40:39

标签: c# active-directory organizational-unit

我试图为新创建的用户设置用户属性。像samaccount和userprincipalname这样的属性可以正常工作,但地址和电话号码等其他属性并不适用。我正在使用文本框。这是一个属性示例:

newUser.Properties["givenName"].Value = txt.FName

我得到的错误是该字段无效,但上面提到的其他字段确实有效。谁能解释为什么会这样?

2 个答案:

答案 0 :(得分:0)

我认为这有助于你......

  public void SetAdInfo(string objectFilter, Property objectName, 
            string objectValue, string LdapDomain)
  {
    string connectionPrefix = "LDAP://" + LdapDomain;
    DirectoryEntry entry = new DirectoryEntry(connectionPrefix);
    DirectorySearcher mySearcher = new DirectorySearcher(entry);
    mySearcher.Filter = "(cn=" + objectFilter + ")";
    mySearcher.PropertiesToLoad.Add(""+objectName+"");
    SearchResult result = mySearcher.FindOne();
    if (result != null)
    {
        DirectoryEntry entryToUpdate = result.GetDirectoryEntry();
        if (!(String.IsNullOrEmpty(objectValue)))
        {
            if (result.Properties.Contains("" + objectName + ""))
            {
                entryToUpdate.Properties["" + objectName + ""].Value = objectValue;
            }
            else
            {
                entryToUpdate.Properties["" + objectName + ""].Add(objectValue);
            }
            entryToUpdate.CommitChanges();
        }
    }
    entry.Close();
    entry.Dispose();
    mySearcher.Dispose();
}

您可以查看此文章以获取更多信息: https://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C

答案 1 :(得分:0)

创建用户的完整代码是:

    private void btn_AddStudent_Click(object sender, EventArgs e)
    {
        try
        {

            // Username en wachtwoord in variabelen zetten.
            string userName = generator(8);
            string password = generator(8);

            // Juiste OU pad aangeven. Afhankelijk van geselecteerde richting.
            string ouString = "OU = " + cmb_Study.Text;
            string LDAPstring = "LDAP://" + ouString + ", DC=DR, DC=GUI";
            DirectoryEntry dirEntry = new DirectoryEntry(LDAPstring);

            // User aanmaken.
            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});
            newUser.Properties["initials"].Value = txt_Initials;
            newUser.Properties["Given-Name"].Value = txt_FName;
            newUser.Properties["sn"].Value = txt_LName;
            newUser.Properties["mail"].Value = txt_Mail;
            newUser.Properties["mobile"].Value = txt_Mobile;
            newUser.Properties["telephoneNumber"].Value = txt_Telephone;
            newUser.Properties["streetAddress"].Value = txt_Street + " " + txt_Number;
            newUser.Properties["postalCode"].Value = txt_PostalCode;

            newUser.Close();
            dirEntry.Close();
            newUser.Dispose();
            dirEntry.Dispose();
            MessageBox.Show("User has been succesfully added");

在这段代码下可以捕捉到它。