Add user to group in active directory using C#.Net


In this article, we will discuss how we can add a user to an Active directory using C#.Net code. Also, you can check out my previous posts on:
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;
    }
}
Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →