Windows 函数 "NetUserChangePassword" 在 Windows 10 下不再工作(在 Windows 7 下工作)

Windows function "NetUserChangePassword" is no longer working under Windows 10 (worked under Windows 7)

本文关键字:Windows 工作 函数 NetUserChangePassword 不再      更新时间:2023-10-16

我正在使用MSDN页面中的以下代码示例来更改用户的网络密码(https://learn.microsoft.com/en-us/windows/win32/api/lmaccess/nf-lmaccess-netuserchangepassword):

#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "netapi32.lib")
#include <stdio.h>
#include <windows.h> 
#include <lm.h>
int wmain(int argc, wchar_t *argv[])
{
DWORD dwError = 0;
NET_API_STATUS nStatus;
//
// All parameters are required.
//
if (argc != 5)
{
fwprintf(stderr, L"Usage: %s \\ServerName UserName OldPassword NewPasswordn", argv[0]);
exit(1);
}
//
// Call the NetUserChangePassword function.
//
nStatus = NetUserChangePassword(argv[1], argv[2], argv[3], argv[4]);
//
// If the call succeeds, inform the user.
//
if (nStatus == NERR_Success)
fwprintf(stderr, L"User password has been changed successfullyn");
//
// Otherwise, print the system error.
//
else
fprintf(stderr, "A system error has occurred: %dn", nStatus);
return 0;
}

在Windows7下,可以使用此程序更改用户的密码。示例:

> NetUserChangePassword.exe \192.168.1.2 TestUser TestPassword TestPassword  
> User password has been changed successfully

在Windows10下,完全相同的程序不再工作:

> NetUserChangePassword.exe \192.168.1.2 TestUser TestPassword TestPassword 
> A system error has occurred: 5

两台计算机(Win7和Win10(上的安全设置相同,并且防火墙已关闭。关于如何解决这个问题的想法将不胜感激!

如果您正在为Active Directory编程,您可以调用IADsUser::ChangePassword方法来获得与调用NetUserChangePassword函数相同的结果。

如果应用程序在运行Active Directory的域控制器上调用NetUserChangePassword函数,则默认ACL仅允许域管理员帐户操作员调用此函数。

在成员服务器或工作站上,只有管理员超级用户才能调用此函数。

用户可以更改自己的密码。

在某些情况下,调用NetUserChangePassword函数的进程还必须启用SE_CHANGE_NOTIFY_NAME权限。

因此,您需要检查两个信息:

  1. 是否使用Active Directory
  2. 运行程序的帐户是否满足所需权限?例如,要查找帐户所扮演的角色,可以使用以下代码示例(在C#中(。参见WindowsPrincipal.IsInRole方法。

    AppDomain myDomain = Thread.GetDomain();
    myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
    WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
    Console.WriteLine("{0} belongs to: ", myPrincipal.Identity.Name.ToString());
    Console.WriteLine("{0}? {1}.", "Domain Admins",
    myPrincipal.IsInRole("Domain Admins"));
    Console.WriteLine("{0}? {1}.", "AccountOperator",
    myPrincipal.IsInRole(WindowsBuiltInRole.AccountOperator));
    Console.WriteLine("{0}? {1}.", "Administrators",
    myPrincipal.IsInRole(WindowsBuiltInRole.Administrator));
    Console.WriteLine("{0}? {1}.", "PowerUser",
    myPrincipal.IsInRole(WindowsBuiltInRole.PowerUser));
    
相关文章: