【问题标题】:C# Active Directory setting user propertiesC# Active Directory 设置用户属性
【发布时间】:2018-04-08 19:31:56
【问题描述】:

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

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

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

【问题讨论】:

  • 你是否检查过“givenname”是否存在于活动目录 DirectoryEntry 属性集合中......
  • 我对 C# 还是很陌生。我该如何检查?
  • 调试 :) 看看“newuser”
  • 你能贴出代码吗?

标签: c# active-directory organizational-unit


【解决方案1】:

我认为这对你有帮助..

  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

【讨论】:

    【解决方案2】:

    创建用户的完整代码如下:

        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");
    

    那段代码下有一个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多