Programattically Change Password

  • Thread starter Thread starter MikeL
  • Start date Start date
M

MikeL

Hello.

I need to programmatically reset a user's password. We will create an
account that has the proper authority to do so.

I've been looking through the .Net framework for a class that might handle
this but am having no luck.

Does anyone have any suggestions?

Thanks in advance,

Mike
 
Hi,

I need to programmatically reset a user's password. We will create an
account that has the proper authority to do so.

I've been looking through the .Net framework for a class that might handle
this but am having no luck.

Does anyone have any suggestions?

You can do that through Active Directory (you don't need to actually have
an Active Directory server for this to work, it works fine with the local
Windows accounts).

You'll need to add a reference to the System.DirectoryServices assembly to
your project.


using System.DirectoryServices;

....

// That's the local Active Directory
DirectoryEntry localDirectory = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");

DirectoryEntry user = null;
string username = "John";
string password = "newPassword";

//Look for the user on the local machine
try
{
user = localDirectory.Children.Find(username, "user");
}
catch(Exception ex)
{
// This user doesn't exist or something wrong happened
}

// Set new password for this user
user.Invoke("SetPassword", new Object[] {password});
user.CommitChanges();
 
Hi, Mehdi. Thanks for the response.

That works as long as the account that I'm trying to change is the same
account as the user issuing the command. When I ran my program, which tried
to change the password on a different account, I got an error stating
something to the effect of resources being allocated to a different account,
and that I needed to disconnect and reconnect. I think it's because I was
signed on as me, and I was trying to change another user's account.

My app is sort of a service, so it is not running under the account whose
password needs to be changed.

Any thoughts of what I need to do?

Thanks again,

Mike

Mehdi said:
Hi,

I need to programmatically reset a user's password. We will create an
account that has the proper authority to do so.

I've been looking through the .Net framework for a class that might
handle
this but am having no luck.

Does anyone have any suggestions?

You can do that through Active Directory (you don't need to actually have
an Active Directory server for this to work, it works fine with the local
Windows accounts).

You'll need to add a reference to the System.DirectoryServices assembly to
your project.


using System.DirectoryServices;

...

// That's the local Active Directory
DirectoryEntry localDirectory = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");

DirectoryEntry user = null;
string username = "John";
string password = "newPassword";

//Look for the user on the local machine
try
{
user = localDirectory.Children.Find(username, "user");
}
catch(Exception ex)
{
// This user doesn't exist or something wrong happened
}

// Set new password for this user
user.Invoke("SetPassword", new Object[] {password});
user.CommitChanges();
 
Back
Top