我試圖打電話NetUserChangePassword
更改遠程計算機上的密碼。當我登錄到機器時,我可以更改密碼,但我無法通過代碼進行操作。返回值爲2245,相當於密碼太短。需要一種方法來更改遠程用戶密碼 - 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 = "[email protected]!";
string newPassword = "!B3tt3r-Luck2";
uint result = ChangeUserPassword(computer, userName,
oldPassword, newPassword);
MessageBox.Show(result.ToString());
}
public Form1()
{
InitializeComponent();
}
}
我認爲這可能也是。我將它改爲完全獨特的東西,但仍然失敗。 (謝謝你的想法)。 – Vaccano 2009-07-15 22:45:24