Click here to Skip to main content
Click here to Skip to main content

Add User to Active Directory

, 28 Jan 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple application for adding users to active directory.


Add users to active directory without installing windows server or active directory roles and features.

Using the code

To add a new user to Active Directory we use three classes:

  1. System.Net.NetworkInformation: We want to add another user to our domain therefore at first we should find out our domain name. We find its name by using this code:
  2. string stringDomainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
  3. PrincipalContext: is a container of domain which all operations are performed against it. To add another user, PrincipalContext uses credential specified by username and password. This credential is used to connect to active directory therefore the user with this credential must has permission to add users.
  4. UserPrincipal: Making another user and add it to active directory is very simple. We just need to initialize an instance of UserPrincipal by using the specified context from the pervious part and also username and password of new user. Then we just assign input textboxes data to related properties of UserPrincipal.
private void button1_Click(object sender, RoutedEventArgs e)
        string stringDomainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
        PrincipalContext PrincipalContext4 = new PrincipalContext(ContextType.Domain, stringDomainName, 
          textboxOu.Text, ContextOptions.SimpleBind, textboxAdminUsername.Text, passwordboxAdminPassword.Password);
        UserPrincipal UserPrincipal1 = new UserPrincipal(PrincipalContext4, 
          textboxLonOnName.Text, passwordboxUserPass.Password, true);
        //User Logon Name
        UserPrincipal1.UserPrincipalName = textboxSamAccountName.Text;
        UserPrincipal1.Name = textboxName.Text;
        UserPrincipal1.GivenName = textboxGivenName.Text;
        UserPrincipal1.Surname = textboxSurname.Text;
        UserPrincipal1.DisplayName = textboxDisplayName.Text;
        UserPrincipal1.Description = textboxDescription.Text;
        if (radiobuttonEnable.IsChecked == true)
            UserPrincipal1.Enabled = true;
            UserPrincipal1.Enabled = false;
        MessageBox.Show("Saved Sucessfully");
    catch (Exception ex)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chief Technology Officer National Iranian Oil Company (NIOC)
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
- Timeline of my career focus:
1994: Batch files in Microsoft Dos
1995: GW-Basic, Q Basic
1996: Assembly, C
1999: Pascal, C++
2000: Bachelor of Science Degree in Computer Software Engineering
2001: Borland Delphi, Microsoft SQL Server, HTML
2002: Visual Basic, C--, Ada, Lisp, Prolog, JavaScript
2003: C#, Java
2004:, Computer Networks
2005: Master of Science Degree in Computer Software Engineering
2006: Data Mining
2007: XML, XQuery, XSLT
2008: WPF
2009: Silverlight, Network Administration
2010: ASP.NET MVC, JQuery, CSS, Ajax, LINQ, Entity Framework
2011: VMware Visualization (vSphere, vCenter)
2012: Microsoft SharePoint, Microsoft System Center, Microsoft Visualization (Hyper-V), Microsoft Exchange Server, Microsoft Lync, Windows 8 App
2013: Cloud Computing (VMware and Microsoft Hyper-V),
2014: ???

Comments and Discussions

Questionadd user to ldap by page Pinmembergprsm27-Apr-14 22:05 
Questionother thing PinmemberEmmanuel Cerna19-Mar-14 14:42 
AnswerRe: other thing PinmemberMehdiNaseri19-Mar-14 23:08 
QuestionHelp ussing your code. PinmemberEmmanuel Cerna19-Mar-14 13:32 
AnswerRe: Help ussing your code. PinmemberMehdiNaseri25-Mar-14 22:30 
QuestionLoading your AD user adder Source code PinprofessionalMember 1036935830-Dec-13 19:45 
AnswerRe: Loading your AD user adder Source code PinmemberMehdiNaseri1-Jan-14 22:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 28 Jan 2013
Article Copyright 2013 by MehdiNaseri
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid