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

Add User to Active Directory

By , 28 Jan 2013
 

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130617.1 | Last Updated 28 Jan 2013
Article Copyright 2013 by MehdiNaseri
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid