5 Comments

 

If you want to rename an user in Active Directory by using C#, you can use the following code:

using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("This application will rename a user in active directory.");
                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("Enter domain:");
                string domain = Console.ReadLine();

                Console.WriteLine("Connecting to domaincontroller.");
                using (PrincipalContext context = new PrincipalContext(ContextType.Domain, domain))
                {
                    if (context == null)
                    {
                        throw new ApplicationException("Domain not found.");
                    }

                    Console.WriteLine("Enter current username (e.g. john):");
                    string currentUserName = Console.ReadLine();
                                        
                    using (UserPrincipal user = UserPrincipal.FindByIdentity(context, currentUserName))
                    {
                        if (user == null)
                        {
                            throw new ApplicationException("User not found.");
                        }

                        Console.WriteLine("Enter new username (e.g. john2):");
                        string newUserName = Console.ReadLine();

                        using (DirectoryEntry entry = (DirectoryEntry)user.GetUnderlyingObject())
                        {
                            Console.WriteLine("Setting account properties in active directory.");
                            entry.InvokeSet("uid", newUserName);
                            entry.InvokeSet("sAMAccountName", newUserName);
                            entry.InvokeSet("userPrincipalName", string.Format("{0}@{1}", newUserName, domain));
                            entry.CommitChanges();

                            Console.WriteLine("Rename common-name (CN).");
                            entry.Rename("CN=" + newUserName);
                            entry.CommitChanges();

                            Console.WriteLine("User successfully renamed.");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            
            Console.WriteLine("Press enter to continue...");
            Console.ReadLine();
        }
    }
}

5 Replies to “How to rename an user in Active Directory with C#”

  1. Hi, I am getting error like below

    An exception of type ‘System.UnauthorizedAccessException’ occurred in System.DirectoryServices.dll but was not handled in user code.

    How to check the user authorization.

  2. I am getting exception: “The attribute syntax specified to the directory service is invalid” when invoking CommitChanges() after making the changes in InvokeSet()

  3. Sorry for the previous comment. the code is working, (I did not follow it %100)
    Thanks so much!

  4. It is not necessary to rename the CN= . I don’t understand why so many people try to do this? When you look inside the AD you will notice that all the accounts you renamed appear in the console with the same ugly account name. You want the CN to match the displayName property. Or leave it as it is! Additionally uID is not required to be set. sAMAccountName rename is all you need

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

JSON Serialization and Deserialization

0 Comments

Found a good articale on JSON Serialization and Deserialization: http://www.codeproject.com/Articles/272335/JSON-Serialization-and-Deserialization-in-ASP-NET…