简体   繁体   English

C#Active Directory设置用户属性

[英]C# Active Directory setting user properties

I'm trying to set user properties for a newly created user. 我正在尝试为新创建的用户设置用户属性。 Properties like samaccount and userprincipalname work but other properties like address and phone number don't. 诸如samaccount和userprincipalname之类的属性可以工作,而地址和电话号码之类的其他属性却不起作用。 I'm making use of textboxes. 我正在使用文本框。 Here's a property example: 这是一个属性示例:

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

The error I get is that the field is invalid yet the other fields named above do work. 我得到的错误是该字段无效,但上面命名的其他字段仍然有效。 Could anyone explain why this is? 谁能解释为什么?

I think this help you out.. 我认为这可以帮助您。

  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();
}

You can check this article for more info: https://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C 您可以查看本文以获取更多信息: https : //www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C

The full code that creates a user is this: 创建用户的完整代码是这样的:

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

There's a catch under that piece of code that does work. 在这段有效的代码下有一个陷阱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM