Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a new group in Active Directory using LDAP in C#

I have scenerio to create new groups in Active Directory using LDAP and C#.

Please provide the suggestions

like image 250
Thangamani Palanisamy Avatar asked Apr 23 '13 14:04

Thangamani Palanisamy


People also ask

What is LDAP authentication in C#?

LDAP. We have an web application developed using c#(VS 2008/3.5 framework). The application uses the mode of authentication as "Windows" with a service account present in domain (Domain1) to run the application as ASP.Net user. We have authentication to be done for the users present in different domain (Domain 2).

What is C# DirectoryEntry?

The DirectoryEntry class presents a node or object in the Active Directory hierarchy. The Add method creates a request to create a new entry in the container. The Find method returns the child with the specified name. The Remove method deletes a child DirectoryEntry from this collection.

What is System DirectoryServices AccountManagement?

System. DirectoryServices. AccountManagement manages directory objects independent of the System.


2 Answers

This article on CodeProject is a really good starting point:

Howto: (Almost) Everything In Active Directory via C#

To create a group, you need to:

  • bind to a container where you want to create the group inside of
  • create the group and define some properties

Code:

public void Create(string ouPath, string name)
{
    if (!DirectoryEntry.Exists("LDAP://CN=" + name + "," + ouPath))
    {
        try
        {
            // bind to the container, e.g. LDAP://cn=Users,dc=...
            DirectoryEntry entry = new DirectoryEntry("LDAP://" + ouPath);

            // create group entry
            DirectoryEntry group = entry.Children.Add("CN=" + name, "group");

            // set properties
            group.Properties["sAmAccountName"].Value = name;

            // save group
            group.CommitChanges();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message.ToString());
        }
    }
    else { Console.WriteLine(path + " already exists"); }
}
like image 165
marc_s Avatar answered Sep 21 '22 13:09

marc_s


Some addition info on setting the Group Scope and Group Type, the enums are:

public enum GroupType : uint
{
    GLOBAL       = 0x2,
    DOMAIN_LOCAL = 0x4,
    UNIVERSAL    = 0x8,
    SECURITY     = 0x80000000
}

SECURITY ( Shortened from ADS_GROUP_TYPE_SECURITY_ENABLED ) is combined with the first 3 enums to give you the 6 possible options, without it a group will be a Distribution group.

The values are set as an int, which with the security flag goes into negatives, so unchecked() needs to be used. Alternatively you could create an enum for the combined values.

GLOBAL       | SECURITY = 0x80000002 = -2147483646
DOMAIN_LOCAL | SECURITY = 0x80000004 = -2147483644
UNIVERSAL    | SECURITY = 0x80000008 = -2147483640

The value is stored in the 'groupType' property:

var groupType = unchecked((int)(GroupType.UNIVERSAL | GroupType.SECURITY));
group.Properties["groupType"].Value = groupType;
group.CommitChanges();
like image 39
WhoIsRich Avatar answered Sep 21 '22 13:09

WhoIsRich