0

the main problem is that windows will in some future disable to modify users password like this method below, and I need to get some other approach beside this one.

static void batchCMD(string user, string newPass)
    {

        try
        {
            Process cmdProcess = new Process();
            cmdProcess.StartInfo.FileName = "cmd.exe";
            if (System.Environment.OSVersion.Version.Major >= 6)
            {
                cmdProcess.StartInfo.Verb = "runas";
            }
            cmdProcess.StartInfo.CreateNoWindow = true;
            cmdProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            cmdProcess.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory();

            cmdProcess.StartInfo.Arguments = "/c net user " + user + " " + newPass;
            cmdProcess.Start();

            cmdProcess.WaitForExit(10000);
            return;
        }
        catch (Exception)
        {

            throw;
        }
        finally
        {
            File.WriteAllText(@"C:\temp\txt.log", DateTime.Now + " " + "HIUHIUHIUH" + "\n");
        }
    }

If you have some other and better solution please share with me.

  • I don't know for sure but I'd be surprised if you can't do it via PowerShell, maybe look it up – ADyson Jun 13 '19 at 13:37
  • *windows will in some future disable to modify users password like this*. Where did you read that? – dotNET Jun 13 '19 at 13:40
  • Have you done any research whatsoever? It seems like you'd find the answer pretty quickly with a simple Google search. – rory.ap Jun 13 '19 at 13:41
  • @dotNET The `NET` command-line program has had a number of features removed or deprecated in recent Windows releases, so it's reasonable to assume that Windows' command-line interface isn't as stable as the Win32 API. Examples include `net name`, `net print`, `net send`, `net start/stop`, etc. – Dai Jun 13 '19 at 13:45
  • @Dai That *still* does not answer dotNET's question. – Camilo Terevinto Jun 13 '19 at 13:48
  • @CamiloTerevinto yea you was right I managed to find the solution that I need. – Dusan Nikolic Jun 13 '19 at 14:08

1 Answers1

0

Use the Win32 NetUserChangePassword function: https://learn.microsoft.com/en-us/windows/desktop/api/lmaccess/nf-lmaccess-netuserchangepassword

The C# P/Invoke signature is available here: https://www.pinvoke.net/default.aspx/netapi32.netuserchangepassword

[DllImport( "netapi32.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, SetLastError = true )]
static extern UInt32 NetUserChangePassword
(
    [MarshalAs( UnmanagedType.LPWStr )] String domainName,
    [MarshalAs( UnmanagedType.LPWStr )] String userName,
    [MarshalAs( UnmanagedType.LPWStr )] String oldPassword,
    [MarshalAs( UnmanagedType.LPWStr )] String newPassword
);
Dai
  • 141,631
  • 28
  • 261
  • 374