Jump to content

Recommended Posts

Posted

Hi All,

 

So i have been playing with a powershell to reset a domain user password. which worked perfectly.

Now trying to create it in c# with visual studio (nicer for the other people who will need to change a password), but can seem to figure out where its going wrong.

 

Any off you able to lay fresh eyes on it?

 

private void button1_Click(object sender, EventArgs e)

{

string username = UserTxtbox.Text;

string newpassword = PwdTxtbox.Text;

{

DirectoryEntry user = new DirectoryEntry(username);

user.Invoke("SetPassword", new object[] { newpassword });

user.Close();

}

}

 

Here is the powershell version i have working

 

$password = (ConvertTo-SecureString -AsPlainText "Passw0rd123" -Force)

$credential = New-Object System.Management.Automation.PSCredential "domain.local\admin",$password

 

$user = Read-Host "Type username:"

$newpassword = Read-Host "Type password:" -AsSecureString

 

Set-ADAccountPassword –Identity $user -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $newpassword –Force)

  • 3 weeks later...
Posted

Try:

 

       private bool ResetUserPassword
       {
           get
           {
               try
               {
                   // Create an Instance of DirectoryEntry.
                   DirectoryEntry myDirectoryEntry = new DirectoryEntry("LDAP://domain.local");
                   myDirectoryEntry.Username = this.UserTxtbox.Text;
                   myDirectoryEntry.Password = this.PwdTxtbox.Text;
               }
               catch (Exception)
               {
                   return false;
               }
               return true;
           }
       }

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...