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;
elselblLoginErrorDetails.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();
}
}
}
Bechir BejaouiPosted Feb 1, 2009, 5:46 PM
public class ActivDirectory
{
public string User{get;set;}
public string Passwd{get;set;}
public string path{get;set;}
public ActivDirectory(string User,string Passwd,string path)
{
this.User = User;
this.Passwd = Passwd;
this.path = path;
}
public void CreateUserAccountAd()
{
DirectoryEntry dentry = new DirectoryEntry(path);
DirectoryEntries chdren = dentry.Children;
DirectoryEntry duser = chdren.Add(User,"user");
duser.Properties["sAMAccountName"].Add(User);
duser.CommitChanges();
SetPassword(duser, Passwd);
Enable(duser);
}
private static void SetPassword(DirectoryEntry directoryentry, string password)
{
object[] PwdArray = new object[] {password};
object result = directoryentry.Invoke("SetPassword", PwdArray );
directoryentry.CommitChanges();
}
public void Enable(DirectoryEntry dr)
{
ActiveDs.IADsUser user1 = (ActiveDs.IADsUser) dr.NativeObject;
user1.AccountDisabled =false;
dr.CommitChanges();
}
}
try this ; - ) jamila hath sayd!!