我正在尝试调用NetUserChangePassword
以更改远程计算机上的密码。登录机器时可以更改密码,但无法通过代码更改密码。返回值为 2245,相当于密码太短。
我阅读了此链接: http: //support.microsoft.com/default.aspx ?scid=kb;en-us;131226但链接中的任何内容都对我没有帮助。(我的代码似乎不符合所指出的任何问题。)
如果您对如何修复此错误有任何想法或有其他方法以编程方式更改远程(Windows 2003)机器上的用户密码,我将不胜感激。
我在 windows xp 机器上运行代码。
这是我当前的代码,以防万一它有用(还显示了我的创建用户代码,它工作得很好)。
public partial class Form1 : Form
{
[DllImport("netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int NetUserAdd(
[MarshalAs(UnmanagedType.LPWStr)] string servername,
UInt32 level,
ref USER_INFO_1 userinfo,
out UInt32 parm_err);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct USER_INFO_1
{
[MarshalAs(UnmanagedType.LPWStr)]
public string sUsername;
[MarshalAs(UnmanagedType.LPWStr)]
public string sPassword;
public uint uiPasswordAge;
public uint uiPriv;
[MarshalAs(UnmanagedType.LPWStr)]
public string sHome_Dir;
[MarshalAs(UnmanagedType.LPWStr)]
public string sComment;
public uint uiFlags;
[MarshalAs(UnmanagedType.LPWStr)]
public string sScript_Path;
}
[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);
// Method to change a Password of a user on a remote machine.
private static uint ChangeUserPassword(string computer, string userName,
string oldPassword, string newPassword)
{
return NetUserChangePassword(computer, userName,
oldPassword, newPassword);
}
// Method used to create a new user on a Remote Machine
private static uint CreateUser(string computer, string userName,
string password)
{
const int UF_DONT_EXPIRE_PASSWD = 0x10000;
const int UF_ACCOUNTDISABLE = 0x000002;
const int USER_PRIV_GUEST = 0; // lmaccess.h:656
const int USER_PRIV_USER = 1; // lmaccess.h:657
const int USER_PRIV_ADMIN = 2; // lmaccess.h:658
USER_INFO_1 userinfo = new USER_INFO_1()
{
sComment = "Scan Track User",
sUsername = userName,
sPassword = password,
sHome_Dir = "",
sScript_Path = "",
uiPriv = USER_PRIV_USER,
uiFlags = UF_DONT_EXPIRE_PASSWD
};
uint output;
NetUserAdd(computer, 1, ref userinfo, out output);
return output;
}
private void button1_Click(object sender, EventArgs e)
{
string computer = "10.1.9.115";
string userName = "test2";
string psswrd = "ssssss";
string fullname = "";
uint output = CreateUser(computer, userName, psswrd);
MessageBox.Show(output.ToString());
}
private void button2_Click(object sender, EventArgs e)
{
string computer = "10.1.9.115";
string userName = "test";
string oldPassword = "!B3tt3rLuck!@!";
string newPassword = "!B3tt3r-Luck2";
uint result = ChangeUserPassword(computer, userName,
oldPassword, newPassword);
MessageBox.Show(result.ToString());
}
public Form1()
{
InitializeComponent();
}
}