Wednesday, August 29, 2007

Provisioning AD users

//Provisioning AD Users from C#
Include directory services from ADD reference

protected void Button1_Click(object sender, EventArgs e)
{
string ldapPath = "LDAP://OU=PMPMIIS,OU=Fabrikam,DC=fabrikam,dc=com";
string status = ProvisonADUser(TextBox1.Text, TextBox2.Text, ldappath);
}



string ProvisonADUser(string username, string password, string ldapPath)
{
try
{
string oGUID = string.Empty;
DirectoryEntry dirEntry = new DirectoryEntry(ldapPath);
DirectoryEntry newUser = dirEntry.Children.Add("CN=" + username, "User");
newUser.Properties["samAccountName"].Value = username;
newUser.CommitChanges();
oGUID = newUser.Guid.ToString();
newUser.Invoke("SetPassword", new object[] { password });
newUser.CommitChanges();
dirEntry.Close();
newUser.Close();
string Success = "User Provisioned Successfully";
return Success;
}
catch (System.DirectoryServices.DirectoryServicesCOMException E)
{
string Error = E.Message;
return Error;
}
}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home