Aswathy
Aswathy

Reputation: 29

Creating a new group in Active Directory

I am working with a C# Windows application. I want to implement Active Directory authentication in that project. But I don't know how to create a group in Active Directory.

How to create a new group in Active Directory via c# code?

Upvotes: 2

Views: 3106

Answers (1)

marc_s
marc_s

Reputation: 754220

If you're on .NET 3.5 and up, you should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:

Basically, you need to establish a principal context on the container where you want to create the group inside of, and then create a new GroupPrincipal, set it's properties, and save it:

// set up domain context and binding to the OU=TechWriters organizational unit in your company
using(PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YourCompany", "ou=TechWriters,dc=yourcompany,dc=com"))
{
    // create a new group principal, give it a name
    GroupPrincipal group = new GroupPrincipal(ctx, "Group01"); 

    // optionally set additional properties on the newly created group here....

    // save the group 
    group.Save();
}

The new S.DS.AM makes it really easy to play around with users and groups in AD!

Upvotes: 4

Related Questions