10

how to change password to user account, by c# code?

4
  • 2
    Which user's account? Domain? Any App? System? Commented Nov 23, 2010 at 8:02
  • Might be a dupe of this question (depending on what password you want to change): stackoverflow.com/questions/234845/… Commented Nov 23, 2010 at 8:06
  • and this stackoverflow.com/questions/384304/… Commented Nov 23, 2010 at 8:07
  • I use with the link that ho1 gave, It works well, then thank you Commented Nov 23, 2010 at 9:23

4 Answers 4

10

Here is a simpler way to do this, however you will need to reference System.DirectoryServices.AccountManagement from .Net 4.0

namespace PasswordChanger
{
    using System;
    using System.DirectoryServices.AccountManagement;

    class Program
    {
        static void Main(string[] args)
        {
            ChangePassword("domain", "user", "oldpassword", "newpassword");
        }

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

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Works great even without admin privileges, thanks Paul.
I got an exception AccessDenied even when I ran the app as administrator. I ended up using the solution here: stackoverflow.com/questions/7567701/…
5

Using active directory:

// Connect to Active Directory and get the DirectoryEntry object.
// Note, ADPath is an Active Directory path pointing to a user. You would have created this
// path by calling a GetUser() function, which searches AD for the specified user
// and returns its DirectoryEntry object or path. See http://www.primaryobjects.com/CMS/Article61.aspx
DirectoryEntry oDE;
oDE = new DirectoryEntry(ADPath, ADUser, ADPassword, AuthenticationTypes.Secure);

try
{
   // Change the password.
   oDE.Invoke("ChangePassword", new object[]{strOldPassword, strNewPassword});
} 
catch (Exception excep)
{
   Debug.WriteLine("Error changing password. Reason: " + excep.Message);
}

Here you have example to change it in the local user account:

http://msdn.microsoft.com/en-us/library/ms817839

Other alternative could be using interoperability and call unmanaged code: netapi32.dll

http://msdn.microsoft.com/en-us/library/aa370650(VS.85).aspx

2 Comments

Thanks for your help, how can I get ADPath? need to use with ActiveDS, If so, how?
Sorry, the link on the code is hidden, maybe stack overflow could improve this :-) Here is the link to the general use of AD and how to build the ADPath: primaryobjects.com/CMS/Article61.aspx
1
        DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
        DirectoryEntry grp;
        grp = AD.Children.Find("test", "user");
        if (grp != null)
        {
            grp.Invoke("SetPassword", new object[] { "test" });
        }
        grp.CommitChanges();
        MessageBox.Show("Account Change password Successfully");

"run in administrator to change all user

Comments

0

This works for both AD and Local Accounts.

If you want to invoke this API via C#, you may use this signature to import API NetUserChangePassword to your C# code:

[DllImport("netapi32.dll", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall,SetLastError=true )]
static extern uint NetUserChangePassword (

[MarshalAs(UnmanagedType.LPWStr)] string domainname,

[MarshalAs(UnmanagedType.LPWStr)] string username,

[MarshalAs(UnmanagedType.LPWStr)] string oldpassword,

[MarshalAs(UnmanagedType.LPWStr)] string newpassword);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.