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

Add User to Active Directory

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

Introduction

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)
{
    try
    {
        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;
        }
        else
        {
            UserPrincipal1.Enabled = false;
        }
        UserPrincipal1.Save();
        MessageBox.Show("Saved Sucessfully");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

License

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

About the Author

MehdiNaseri
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: Asp.net, 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), Microsoft Project Server.
2014: ???

Comments and Discussions

 
Questionadd user to ldap by asp.net page Pinmembergprsm27-Apr-14 21:05 
Questionother thing PinmemberEmmanuel Cerna19-Mar-14 13:42 
AnswerRe: other thing PinpremiumMehdiNaseri19-Mar-14 22:08 
QuestionHelp ussing your code. PinmemberEmmanuel Cerna19-Mar-14 12:32 
AnswerRe: Help ussing your code. PinpremiumMehdiNaseri25-Mar-14 21:30 
QuestionLoading your AD user adder Source code PinprofessionalMember 1036935830-Dec-13 18:45 
AnswerRe: Loading your AD user adder Source code PinmemberMehdiNaseri1-Jan-14 21: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 | Mobile
Web04 | 2.8.140721.1 | Last Updated 28 Jan 2013
Article Copyright 2013 by MehdiNaseri
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid