1

我正在尝试为新创建的用户设置用户属性。samaccount 和 userprincipalname 等属性有效,但地址和电话号码等其他属性无效。我正在使用文本框。这是一个属性示例:

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

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

4

2 回答 2

2

我想这可以帮助你..

  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

于 2017-10-27T09:05:52.123 回答
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");

在那段代码下有一个问题。

于 2017-10-27T09:27:00.600 回答