Home > Deveploment and Programming > How to change window account password using C#

How to change window account password using C#

Process:

1. Add the below reference in the project..

using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

2.  Add the below function in the class..

public void ChangePassword(string oldPassword, string newPassword)
{
try
{
using (var context = new PrincipalContext(ContextType.Domain))
using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userNameWithoutDomain))
{
user.ChangePassword(oldPassword, newPassword);
}

}
catch (Exception ex)
{}
}

Pass the old password and new password, that function will check first password policy and if policy meet the requirement than it change otherwise give you descriptive error message.

Advertisements
  1. sumit khatri
    29/12/2012 at 12:03 am

    when i should add the refrence’s
    using System.DirectoryServices;
    using System.DirectoryServices.AccountManagement;
    it will show the error in (DirectoryServices:) you miss an assembly or refrence…. plz help me as soon as… 🙂 tnx in advance…

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: