需要一种更改远程用户密码的方法 - NetUserChangePassword 失败并显示 2245

发布于 2024-07-29 01:25:53 字数 3781 浏览 1 评论 0原文

我正在尝试调用 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();
    }


}

I am trying to call NetUserChangePassword to change the passwords on a remote computer. I am able to change the password when I log-in to the machine, but I can't do it via code. The return value is 2245 which equates to the Password Being too short.

I read this link: http://support.microsoft.com/default.aspx?scid=kb;en-us;131226 but nothing in the link was helpful to me. (My code did not seem to fit any of the issues indicated.)

If you have any ideas how to fix this error or have a different way to programmatically change a users password on a remote (Windows 2003) machine I would be grateful to hear it.

I am running the code on a windows xp machine.

Here is my current code in-case it is helpful (Also shows my create user code which works just fine).

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


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜空下最亮的亮点 2024-08-05 01:25:53

在最初的开发和测试过程中,我也被同样的问题困扰,直到我发现这个 API 的一个未记录的限制 - 您尝试更改的密码实际上必须过期才能成功更改!

I was stumped by the same issue during initial development and testing, until I discovered an undocumented restriction of this API - the password that you are trying to change MUST ACTUALLY BE EXPIRED for the change to succeed!

柠檬 2024-08-05 01:25:53

错误 2245 也可能是密码历史记录问题。 新密码是最近使用过的吗?

编辑:
看起来这个函数在 Server 2003 SP 2 之后就崩溃了。使用文档中的示例从 C++ 调用该函数时,我遇到了同样的错误。 您可能需要使用 NetUserSetInfo。

Error 2245 could also be a password history problem. Is the new password one that was used in the recent past?

Edit:
It looks like this function broke after Server 2003 SP 2. I got the same error when calling the function from C++ using the example in the documentation. You'll probably need to use NetUserSetInfo.

情归归情 2024-08-05 01:25:53

在我安装的 Windows 2008 R2 中,我必须更改 2 个 GPO 才能使 NetUserChangePassword 正常工作。

我必须(通过 GPO)将“最短密码期限”设置为 0,因为我刚刚创建了一个测试帐户,并且在此更改之前的所有尝试都导致了“密码太短”错误代码。

由于我的虚拟机是 DC,因此我必须允许测试用户登录 DC 才能使该方法正常工作。

On my Windows 2008 R2 install, I had to change 2 GPOs to make NetUserChangePassword work.

I had to set (through GPO) the 'minimum password age' to 0, as I had just created a test account and all attempts before this change resulted in the 'password too short' error code.

As my VM is a DC, I had to allow my test users to logon to the DC in order for the method to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文