how to create new user in Active Directory through Ax code

Hi all,

I am trying to access Active Directory through Ax code. I can validate users in Active Directory but, I dont know how to create new user in Active Directory through Ax…PlzHelp

Hi Kevin,

There is an extensive tutorial in C# about dealing with AD.

I’ve modified the user creation code to work in AX. Please remember that for creation to be successful, your user should have privileges to create a user in the AD.

static void CreateUserInActiveDirectory(Args _args)
{
str userName = “NewUser”;
str userPassword = “password”;
str userGUID;
str connection;
System.DirectoryServices.DirectoryEntry directoryEntry;
System.DirectoryServices.DirectoryEntry newUser;
System.DirectoryServices.DirectoryEntries children;
System.Object[] parameters;
;

parameters = new System.Object1;

try
{
parameters.Initialize();
parameters.SetValue(userPassword, 0);
connection = “LDAP://dc=Contoso,dc=com”;
directoryEntry = new System.DirectoryServices.DirectoryEntry(connection);
children = directoryEntry.get_Children();
newUser = children.Add(“CN=” + userName, “user”);
newUser.set_Username(userName);
newUser.CommitChanges();
userGUID = newUser.get_Guid();

newUser.Invoke(“SetPassword”, parameters);
newUser.CommitChanges();
directoryEntry.Close();
newUser.Close();
}
catch (Exception::CLRError)
{
throw error(“An error occurred.”);

}
}

Hi Zubair,

Thank you very much for your response. I tried with your code. It throws the error. Your have given a code to the database connection string as (connection = “LDAP://dc=Contoso,dc=com”;). I am using client environment of Ax and how to get the connection string of the database where i am using in my Server…Thanks for your help in advance…

Hai Zubair,

I used debugger and and it stops at the line newUser = children.Add(“CN=” + userName, “user”);

Also i used the line as ( connection = “LDAP://dc=Unibuzz,dc=com”; ). Do i need a change in this line?

What is the term you used CN here… What i need to change according to my environment as my domain name is Unibuzz…PlZ help…

Hi Kevin,

The CN in the statement means a common name by which we are referring to the user. Try removing that line and instead just use children.Add(userName, “user”);

Your change in the line ( connection = “LDAP://dc=Unibuzz,dc=com”; ) is perfectly correct. DC means domain controller here.

What I’ll suggest is to run the code in C# first. You can get the code by querying Google. That way in C#, you will get the exact exception message and we will know what the problem is.

My guess is you dont have enough rights to create a user in AD as I mentioned earlier.

Hi Zubair,

Thanks for the reply. I tried the code with the server also. But it throws the exception error. I get the code with C# also. But, how to execute that. I tried with Class library, but still it get worse that i cant execute the code with class library. I tried code from the following links:

http://www.codeproject.com/KB/system/everythingInAD.aspx#40

http://www.dotnettreats.com/tipstricks/adnet.aspx

http://dynamicsaxgyan.wordpress.com/2007/05/14/active-directory-in-axapta/

Where i have to try the code…? Again Thanks for your valuable time for me…