Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give path to identify OU when we have nested OU's in active directory while creating a user in active directory in asp.net c#?

Using

PrincipalContext pc = new PrincipalContext(ContextType.Domain, "me.com", "OU=Menetwork OU=Users OU=IT")

I am working on directory programing and I want to know how can I give path for OU when we have nested OUs in active directory while creating a User in active Directory.

like image 577
RL89 Avatar asked Dec 22 '22 01:12

RL89


1 Answers

A Directory is a tree of objects. Each object OUs (containers), user (leaf in your case) is addressed by a distinguished name wich is composed by an attribute=value pair suffixed by the distinguished name of his container. The following two screenshots show you the two visions, MMC one and the LDAP one with all the DNs.

AD MMC visionAD LDP vision

In my case here is how I can create a user in an nested OU like this :

/* Creating a user
 * Retreiving a principal context
 */
PrincipalContext domainContextMonou = new PrincipalContext(ContextType.Domain, "WM2008R2ENT:389", "ou=SousMonou,ou=Monou,dc=dom,dc=fr", "user", "pass");

/* Create a user principal object
 */
UserPrincipal aSlxUser = new slxUser(domainContextMonou, "user3.users", "pass@1w0rd01", true);

/* assign some properties to the user principal
 */
aSlxUser.GivenName = "user3";
aSlxUser.Surname = "users";

/* Force the user to change password at next logon
 */
//aSlxUser.ExpirePasswordNow();

/* save the user to the directory
 */
aSlxUser.Save();

/* set the password to a new value
 */
aSlxUser.SetPassword("test.2013");
aSlxUser.Save(); 
like image 95
JPBlanc Avatar answered Dec 24 '22 01:12

JPBlanc