I use the following code to create a user in Active Directory
DirectoryEntry newUser = null;
try
{
if (!Authenticate()) return null;
newUser = location.Children.Add("CN=" + userName, "user");
newUser.Properties["samAccountName"].Value = userName;
newUser.Properties["cn"].Value = userName;
newUser.CommitChanges();
string guid = newUser.Guid.ToString();
newUser.Invoke("SetPassword", new object[] { password });
newUser.CommitChanges();
DomainDirectoryEntry.Close();
newUser.Close();
return guid;
}
catch
{
throw;
}
finally
{
newUser = null;
}
If I run this code from the asp.net webpage, it will create a user in Active Directory and it will be disabled, but it will throw an exception Exception has been thrown by the target of an invocation., and when I get an internal exception from this exception, I get Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDEN'Make sure the account, which I use to access Active Directory is the full administrator.
I do not determine the reason and why it creates the user, but generates an error when setting the password ... Can someone help me get the error?