locked
How to change the "CN" property of a user!? RRS feed

  • Question

  • User1712486132 posted
    I have a simple problem, but hard to getting the solution:

    I want to change the information of an user in AD, relative to the name of the user. I want to change the following properties:
    • givenName
    • sn
    • displayName
    • sAMAccountName
    • userPrincipalName
    • CN

    The "green" properties i don't have any problem, they are updated in the AD. When i try to change the CN property, i have an exception!!


    <Code>

    DirectoryEntry userEntry = new DirectoryEntry();
    ....
    userEntry.Properties["givenName"].Value = firstName;
    // Last Name
    userEntry.Properties["sn"].Value = lastName;
    // Display Name
    userEntry.Properties["displayname"].Value = nome; //displayName;
    // User Logon Name (Pre-Windows 2000)
    userEntry.Properties["sAMAccountName"].Value = uuleft;
    // User Logon Name
    userEntry.Properties["userPrincipalName"].Value = uu;
    //Update the user
    userEntry.CommitChanges();


    //Change the CN(Name in AD) propertie
    try{
    userEntry.Properties["CN"].Value = uuleft;
    }
    catch(Exception ex){
        //BUG - PROBLEM!!
        Console.Writeln(ex.Message);
    }

    //Update the user
    userEntry.CommitChanges();
    ...
    </Code>

    I even tried to disable the user account and then change the CN property, but with no luck.

    I have one solution... remove the user and then create, but then i have one issue in my hands, what is the password of the user!! :S    BAD SOLUTION

    Therefore, how can i change the CN property of an user in AD!?!

    Thanks in advance.

    Wednesday, February 22, 2006 12:03 PM

All replies

  • User1354132231 posted
    The CN is kind of a special attribute since it helps to form the distinguished name for the object.  You need to use the Rename method if you want to change the CN.  All you do is pass the new RDN to the method:

    DirectoryEntry foo = new DirectoryEntry("LDAP://CN=User Blah,...");
    using (foo){ foo.Rename("CN=New Name"); }


    Wednesday, February 22, 2006 3:39 PM
  • User1712486132 posted
    Thanks Ryan, great tip.

    It works. :)
    Thursday, February 23, 2006 12:07 PM