create new user in active directory

Jan 30 2009 4:01 PM

Hello

I try to add a new user to active directory but  I got an error at the setpassword line. Any comment would be helpfull.

Here is my code:

protected void submit_Click(object sender, EventArgs e)

{

CreateUserAccountAd();

}

public void CreateUserAccountAd()

{

string error = string.Empty;

string strpath = LDAP://server name;

//login as adminsitartor to create a new user

try

{

DirectoryEntry de = new DirectoryEntry(strpath, username, password);

string strUser = "CN=username";

string strPassword = "ssss#1234";

DirectoryEntries child = de.Children;

DirectoryEntry deuser = child.Add(strUser, "user");

deuser.Properties["sAMAccountName"].Add(strUser);

de.CommitChanges();

 SetPassword(deuser, strPassword);

 deuser.Invoke("SetPassword", strPassword);

 Enable(deuser);

deuser.Invoke("SetPassword", new Object[] { strPassword });

 

de.Close();

de.Dispose();

 

}

catch (DirectoryServicesCOMException ex)

{

error = ex.Message;

}

if (error != string.Empty)

lblLoginErrorDetails.Text = error;

else

lblLoginErrorDetails.Text = "Congragulation! You signed up";

}

 

private static void SetPassword(DirectoryEntry UE, string password)

{

object[] oPassword = new object[] { password };

object ret = UE.Invoke("SetPassword", oPassword);

UE.CommitChanges();

}

 

public void Enable(DirectoryEntry dr)

{

ActiveDs.IADsUser user1 = (ActiveDs.IADsUser)dr.NativeObject;

user1.AccountDisabled = false;

dr.CommitChanges();

}

}

}

 


Answers (1)