The
NetUserChangePassword function changes a user's password for a specified network server or domain.
Syntax
NET_API_STATUS NetUserChangePassword(
__in LPCWSTR domainname,
__in LPCWSTR username,
__in LPCWSTR oldpassword,
__in LPCWSTR newpassword
);
Parameters
- domainname [in]
-
Pointer to a constant string that specifies the DNS or NetBIOS name of a remote server or domain on which the function is to execute. If this parameter is NULL, the logon domain of the caller is used.
- username [in]
-
Pointer to a constant string that specifies a user name. The
NetUserChangePassword function changes the password for the specified user.
If this parameter is NULL, the logon name of the caller is used. For more information, see the following Remarks section.
- oldpassword [in]
-
Pointer to a constant string that specifies the user's old password.
- newpassword [in]
-
Pointer to a constant string that specifies the user's new password.
Return Value
If the function succeeds, the return value is NERR_Success.
If the function fails, the return value can be one of the following error codes.
| Return code | Description |
- ERROR_ACCESS_DENIED
| The user does not have access to the requested information.
|
- ERROR_INVALID_PASSWORD
| The user has entered an invalid password.
|
- NERR_InvalidComputer
| The computer name is invalid.
|
- NERR_NotPrimary
| The operation is allowed only on the primary domain controller of the domain.
|
- NERR_UserNotFound
| The user name could not be found.
|
- NERR_PasswordTooShort
| The password is shorter than required. (The password could also be too long, be too recent in its change history, not have enough unique characters, or not meet another password policy requirement.)
|
Remarks
If you are programming for Active Directory, you may be able to call certain Active Directory Service Interface (ADSI) methods to achieve the same functionality you can achieve by calling the network management user functions. For more information, see
IADsUser and
IADsComputer.
If you call this function on a domain controller that is running Active Directory, access is allowed or denied based on the access control list (ACL) for the securable object. The default ACL permits only Domain Admins and Account Operators to call this function. On a member server or workstation, only Administrators and Power Users can call this function. A user can change his or her own password. For more information, see
Security Requirements for the Network Management Functions. For more information on ACLs, ACEs, and access tokens, see
Access Control Model.
The security descriptor of the User object is used to perform the access check for this function. In addition, the caller must have the "Change password" control access right on the User object. This right is granted to Anonymous Logon and Everyone by default.
Note that for the function to succeed, the oldpassword parameter must match the password as it currently exists.
In some cases, the process that calls the
NetUserChangePassword function must also have the SE_CHANGE_NOTIFY_NAME privilege enabled; otherwise,
NetUserChangePassword fails and
GetLastError returns ERROR_ACCESS_DENIED. This privilege is not required for the
LocalSystem account or for accounts that are members of the administrators group. By default, SE_CHANGE_NOTIFY_NAME is enabled for all users, but some administrators may disable the privilege for everyone. For more information about account privileges, see
Privileges and
Authorization Constants.
See
Forcing a User to Change the Logon Password for a code sample that demonstrates how to force a user to change the logon password on the next logon using the
NetUserGetInfo and
NetUserSetInfo functions.
User account names are limited to 20 characters and group names are limited to 256 characters. In addition, account names cannot be terminated by a period and they cannot include commas or any of the following printable characters: ", /, \, [, ], :, |, <, >, +, =, ;, ?, *. Names also cannot include characters in the range 1-31, which are nonprintable.
Examples
The following code sample demonstrates how to change a user's password with a call to the NetUserChangePassword function. All parameters to the function are required.
#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 NewPassword\n", 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 successfully\n");
//
// Otherwise, print the system error.
//
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);
return 0;
}
Requirements
| Minimum supported client | Windows 2000 Professional |
| Minimum supported server | Windows 2000 Server |
| Header | Lmaccess.h (include Lm.h) |
| Library | Netapi32.lib |
| DLL | Netapi32.dll |
See Also
- Network Management
Overview
- Network
Management Functions
- User Functions
- NetUserSetInfo
- NetUserGetInfo
Send comments about this topic to Microsoft
Build date: 11/26/2009