Click here to Skip to main content
15,395,181 members
Articles / Programming Languages / C#
Tip/Trick
Posted 28 Jan 2013

Stats

63.6K views
4.8K downloads
10 bookmarked

Add User to Active Directory

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
28 Jan 2013CPOL
A simple application for adding users to active directory

Introduction

This tip will show how to 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:

C#
string stringDomainName = 
    System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
  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. PrincipalContext: is a container of domain against which all operations are performed. 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 have permission to add users.
  3. UserPrincipal: Making another user and adding 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.
C#
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 Successfully");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

History

  • 28th January, 2013: Initial version

License

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

Share

About the Author

MehdiNaseri
Software Developer (Senior)
Canada Canada
• MCSD (Microsoft Certified Solutions Developer): App Builder
• MCSD (Microsoft Certified Solutions Developer): Web Applications
• MCSA (Microsoft Certified Solutions Associate): Universal Windows Platform
• MCP (Microsoft Certified Professional)

• PMI-PBA (PMI Professional in Business Analysis)
• PMI-ACP (PMI Agile Certified Practitioner)
• PMP (Project Management Professional)

Comments and Discussions

 
QuestionThe username or password incorrect Pin
Kaan Aytekin18-Oct-16 21:20
MemberKaan Aytekin18-Oct-16 21:20 
QuestionNew to asp.net Pin
Member 1145034713-Feb-15 4:41
MemberMember 1145034713-Feb-15 4:41 
Questionadd user to ldap by asp.net page Pin
gprsm27-Apr-14 21:05
Membergprsm27-Apr-14 21:05 
Questionother thing Pin
Emmanuel Cerna19-Mar-14 13:42
MemberEmmanuel Cerna19-Mar-14 13:42 
AnswerRe: other thing Pin
MehdiNaseri19-Mar-14 22:08
professionalMehdiNaseri19-Mar-14 22:08 
QuestionHelp ussing your code. Pin
Emmanuel Cerna19-Mar-14 12:32
MemberEmmanuel Cerna19-Mar-14 12:32 
AnswerRe: Help ussing your code. Pin
MehdiNaseri25-Mar-14 21:30
professionalMehdiNaseri25-Mar-14 21:30 
QuestionLoading your AD user adder Source code Pin
Member 1036935830-Dec-13 18:45
professionalMember 1036935830-Dec-13 18:45 
AnswerRe: Loading your AD user adder Source code Pin
MehdiNaseri1-Jan-14 21:34
professionalMehdiNaseri1-Jan-14 21: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.