First of all import
using System.DirectoryServices;
To add a user to active directory(AD)
public void AddUserToAD(DirectoryEntry de, string[] details)
{
try
{
string oGUID = string.Empty;
domain = ConfigurationManager.AppSettings["DOMAIN"].ToString(); //
de.Username = domain + "\\" + ConfigurationManager.AppSettings["USERNAME"].ToString();
de.Password = ConfigurationManager.AppSettings["PASSWORD"].ToString();
DirectoryEntry newUser = de.Children.Add("CN=" + details[0], "user");
newUser.Properties["samAccountName"].Value = details[0];
newUser.Properties["givenName"].Value = details[0];
newUser.Properties["sn"].Value = details[0];
newUser.Username = domain + "\\" + ConfigurationManager.AppSettings["USERNAME"].ToString();
newUser.Password = ConfigurationManager.AppSettings["PASSWORD"].ToString();
newUser.AuthenticationType = AuthenticationTypes.Secure;
if (details[1] != "")
{
newUser.Properties["TelephoneNumber"].Value = details[1];
}
if (details[2] != "")
{
newUser.Properties["streetAddress"].Value = details[2];
}
if (details[3] != "")
{
newUser.Properties["Description"].Value = details[3];
}
if (details[4] != "")
{
newUser.Properties["mail"].Value = details[4];
}
newUser.CommitChanges();
oGUID = newUser.Guid.ToString();
newUser.Invoke("SetPassword", new object[] { details[5] });
newUser.CommitChanges();
EnableADUser(newUser);
newUser.Close();
}
catch (Exception ex){
Response.Write(ex.Message);
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment