本程序功能类似于Windows提供的更改密码功能。需要提供用户名、旧密码和新密码。但是不会破坏用户的加密证书。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ChangePassword
{
class Program
{
[DllImport("Netapi32.dll")]
extern static int NetUserChangePassword(
[MarshalAs(UnmanagedType.LPWStr)]string sName,
[MarshalAs(UnmanagedType.LPWStr)]string username,
[MarshalAs(UnmanagedType.LPWStr)]string oldPass,
[MarshalAs(UnmanagedType.LPWStr)]string newPass);
static void Main(string[] args)
{
Console.WriteLine("Username:");
string Username = Console.ReadLine();
Console.WriteLine("Old Password");
string OldPassword = Console.ReadLine();
Console.WriteLine("New Password:");
string NewPassword = Console.ReadLine();
Console.WriteLine("Retype Password:");
string NewPassword2 = Console.ReadLine();
if (NewPassword != NewPassword2)
{
Console.WriteLine("两次输入的密码不相同!");
return;
}
try
{
NetUserChangePassword(null, Username, OldPassword, NewPassword);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
}
}
}
}