In this article we will discuss about how we can add an user to Active directory using C#.Net code. Also you can check out my previous posts on:
- Caching in Asp.Net
- Get control value using JavaScript with master page in Asp.Net
- Add ajaxcontroltoolkit to Visual Studio 2012
Below is the full code:
First we need to write using statements like below:
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
public partial class AddUserToAD : System.Web.UI.Page
{
string domain = "UR Domain";
string serviceUser = "ServiceUserAccount";
string servicePassword = "ServicePassword";
string defaultOU = "defaultOU";
protected void Page_Load(object sender, EventArgs e)
{
AddUserToAD ("Test User","Test Group")
}
string AddUserToAD(string userName, string groupName)
{
try
{
UserPrincipal oUserPrincipal = GetUser(userName);
GroupPrincipal oGroupPrincipal = GetGroup(groupName);
if (oUserPrincipal != null || oGroupPrincipal != null)
{
if (!IsUserGroupMember(userName, groupName))
{
oGroupPrincipal.Members.Add(oUserPrincipal);
oGroupPrincipal.Save();
}
}
return "Success";
}
catch (Exception ex)
{
return "Failure";
}
}
private bool IsUserGroupMember(string userName, string groupName)
{
try
{
UserPrincipal oUserPrincipal = GetUser(userName);
GroupPrincipal oGroupPrincipal = GetGroup(groupName);
return oGroupPrincipal.Members.Contains(oUserPrincipal);
}
catch (Exception ex)
{
return false;
}
}
private GroupPrincipal GetGroup(string groupName)
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
GroupPrincipal oGroupPrincipal =
GroupPrincipal.FindByIdentity(oPrincipalContext, sGroupName);
return oGroupPrincipal;
}
private PrincipalContext GetPrincipalContext()
{
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, domain, ConfigurationManager.AppSettings["defaultOUForSearch"], serviceUser, servicePassword);
return principalContext;
}
private UserPrincipal GetUser(string userName)
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, userName);
return oUserPrincipal;
}
}
- Caching in Asp.Net
- Get control value using JavaScript with master page in Asp.Net
- Add ajaxcontroltoolkit to Visual Studio 2012
Below is the full code:
First we need to write using statements like below:
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
public partial class AddUserToAD : System.Web.UI.Page
{
string domain = "UR Domain";
string serviceUser = "ServiceUserAccount";
string servicePassword = "ServicePassword";
string defaultOU = "defaultOU";
protected void Page_Load(object sender, EventArgs e)
{
AddUserToAD ("Test User","Test Group")
}
string AddUserToAD(string userName, string groupName)
{
try
{
UserPrincipal oUserPrincipal = GetUser(userName);
GroupPrincipal oGroupPrincipal = GetGroup(groupName);
if (oUserPrincipal != null || oGroupPrincipal != null)
{
if (!IsUserGroupMember(userName, groupName))
{
oGroupPrincipal.Members.Add(oUserPrincipal);
oGroupPrincipal.Save();
}
}
return "Success";
}
catch (Exception ex)
{
return "Failure";
}
}
private bool IsUserGroupMember(string userName, string groupName)
{
try
{
UserPrincipal oUserPrincipal = GetUser(userName);
GroupPrincipal oGroupPrincipal = GetGroup(groupName);
return oGroupPrincipal.Members.Contains(oUserPrincipal);
}
catch (Exception ex)
{
return false;
}
}
private GroupPrincipal GetGroup(string groupName)
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
GroupPrincipal oGroupPrincipal =
GroupPrincipal.FindByIdentity(oPrincipalContext, sGroupName);
return oGroupPrincipal;
}
private PrincipalContext GetPrincipalContext()
{
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, domain, ConfigurationManager.AppSettings["defaultOUForSearch"], serviceUser, servicePassword);
return principalContext;
}
private UserPrincipal GetUser(string userName)
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, userName);
return oUserPrincipal;
}
}