Click here to Skip to main content
12,822,050 members (42,406 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 28 Jan 2013

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

Software Developer (Senior) National Iranian Oil Company (NIOC)
Iran (Islamic Republic of) Iran (Islamic Republic of)

  • PMI-ACP (PMI Agile Certified Practitioner)

  • PMP (Project Management Professional)

  • MCSD (Microsoft Certified Solutions Developer): App Builder

  • MCSD (Microsoft Certified Solutions Developer): Web Applications

  • MCSA (Microsoft Certified Solutions Associate): Web Applications - Charter Member

  • MCP (Microsoft Certified Professional)

  • Microsoft Specialist: Programming in HTML5 with JavaScript and CSS3

  • MTA (Microsoft Technology Associate)

  • CIW Advanced HTML5 & CSS3 Specialist - Exam 1D0-620

  • CIW Site Development Associate

  • PSM (Professional Scrum Master)

  • PSPO (Professional Scrum Product Owner)

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: Agile, Scrum
2015: Project Management
2016: Security, Android Development

You may also be interested in...


Comments and Discussions

QuestionThe username or password incorrect Pin
Kaan Aytekin18-Oct-16 22:20
memberKaan Aytekin18-Oct-16 22:20 
QuestionNew to Pin
Member 1145034713-Feb-15 5:41
memberMember 1145034713-Feb-15 5:41 
Questionadd user to ldap by page Pin
gprsm27-Apr-14 22:05
membergprsm27-Apr-14 22:05 
Questionother thing Pin
Emmanuel Cerna19-Mar-14 14:42
memberEmmanuel Cerna19-Mar-14 14:42 
AnswerRe: other thing Pin
MehdiNaseri19-Mar-14 23:08
memberMehdiNaseri19-Mar-14 23:08 
QuestionHelp ussing your code. Pin
Emmanuel Cerna19-Mar-14 13:32
memberEmmanuel Cerna19-Mar-14 13:32 
AnswerRe: Help ussing your code. Pin
MehdiNaseri25-Mar-14 22:30
memberMehdiNaseri25-Mar-14 22:30 
QuestionLoading your AD user adder Source code Pin
Member 1036935830-Dec-13 19:45
professionalMember 1036935830-Dec-13 19:45 
AnswerRe: Loading your AD user adder Source code Pin
MehdiNaseri1-Jan-14 22:34
memberMehdiNaseri1-Jan-14 22:34 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 28 Jan 2013
Article Copyright 2013 by MehdiNaseri
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid