?
Solved

Add a user to an active directory group

Posted on 2004-09-03
2
Medium Priority
?
3,268 Views
Last Modified: 2008-02-20
How do I use the IADsGroup.Add function to add a user to a security group in my windows domain.

I have added the user to the domain as follows:

DirectoryEntry de = new DirectoryEntry();

// Set credentials of an AD account that is priveledged to be able to create users
de.Username = username;
de.Password = password;

// Set active LDAP path
de.Path = LDAPpath;

// Assign the users in the LDAPpath to a variable so we can manipulate it (add users)
DirectoryEntries users = de.Children;

// Add user account
DirectoryEntry user = users.Add("CN=" + LastName + "\\, " + FirstName, "user");

// Set additional properties of new account
user.Properties["samAccountName"].Add(username); // Login name
user.Properties["givenName"].Add(FirstName); // First Name
user.Properties["sn"].Add(LastName); // Last Name

// Commit changes
user.CommitChanges();

ActiveDs.IADsUser tester = (ActiveDs.IADsUser)user.NativeObject;
tester.ChangePassword("", Mypassword);

// Commit password changes
user.CommitChanges();


Now how do I add this user to the security group named :

My Standard Group


Thanks!
0
Comment
Question by:mrichmon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
mrichmon earned 0 total points
ID: 11977663
Here is the answer :

DirectoryEntry de = new DirectoryEntry();

// Set credentials of an AD account that is priveledged to be able to create users
de.Username = username;
de.Password = password;

// Set active LDAP path
de.Path = LDAPpath;

// Assign the users in the LDAPpath to a variable so we can manipulate it (add users)
DirectoryEntries users = de.Children;

// Add user account
DirectoryEntry user = users.Add("CN=" + LastName + "\\, " + FirstName, "user");

// Set additional properties of new account
user.Properties["samAccountName"].Add(username); // Login name
user.Properties["givenName"].Add(FirstName); // First Name
user.Properties["sn"].Add(LastName); // Last Name

// Commit changes
user.CommitChanges();

ActiveDs.IADsUser tester = (ActiveDs.IADsUser)user.NativeObject;
tester.ChangePassword("", Mypassword);

// Commit password changes
user.CommitChanges();


// Create a directory entry pointing to the location of the standard OU where the My Standard Group security group is contained
DirectoryEntry groupDE = new DirectoryEntry();

// Set credentials of an AD account to "Run As"
groupDE.Username = username;
groupDE.Password = password;

// Set active LDAP path to be the OU where the security group is contained
groupDE.Path = LDAPpath;

// Find the security group within the OU
DirectoryEntry groupEntry = groupDE.Children.Find("CN=My Standard Group", "group");

// Add the user we just created to the security group
groupEntry.Invoke("add", new object[] {user.Path});

// Commit the changes of adding the user to the security group
groupEntry.CommitChanges();
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question