sari k
sari k

Reputation: 2111

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

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

Upvotes: 10

Views: 33911

Answers (4)

Scott Jasin
Scott Jasin

Reputation: 115

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);

Upvotes: 0

Paul
Paul

Reputation: 1069

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);
            }
        }
    }
}

Upvotes: 10

Programer_saeed
Programer_saeed

Reputation: 133

        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

Upvotes: 1

Daniel Peñalba
Daniel Peñalba

Reputation: 31847

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

Upvotes: 5

Related Questions