
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:
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:
string stringDomainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
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.
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);
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());
}
}
MehdiNaseri
Chief Technology Officer
National Iranian Oil Company (NIOC)
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