0

Does anyone know how can I reset password for Active Directory in C#?

I wrote the following code to reset the password in Active Directory:

   [WebMethod]
   public static string ResetPassword(string test)
   {
       string userDn = "CN=Joe Bloggs,OU=Test Accounts,OU=ST IT,OU=Departments,OU=Internal,OU=Divisions,DC=thegroup,DC=com,DC=au";
       string password = "!qwer12345";

       DirectoryEntry uEntry = new DirectoryEntry(userDn);
       uEntry.Invoke("SetPassword", new object[] { password });
       uEntry.Properties["LockOutTime"].Value = 0; //unlock account

       uEntry.Close();

       return "hello";
   }

When I run the code, an error displays when it reaches the line uEntry.Invoke("SetPassword", new object[] { password });

enter image description here

enter image description here

Does anyone know what's causing this issue and how I could fix it?

Adaline Simonian
  • 4,596
  • 2
  • 24
  • 35
Jin Yong
  • 42,698
  • 72
  • 141
  • 187

1 Answers1

1

This seems to explain the cause of the issue- you need to supply admin credentials (username and password).

I would think it's a security issue - when you're running it locally on Windows directly, it's being executed under your own account, which most likely has enough privileges to create the user.

When you do it through the web service, you run it as the "anonymous" ASP.NET user which by default most likely won't have the permissions to do this.

So when you bind to AD, you will need to supply credentials (username and password) that are sufficiently priviledged to be able to create users. You can do this in the "new DirectoryEntry()" constructor - check it's overloads.

kennyzx
  • 12,845
  • 6
  • 39
  • 83