如何更改用户密码(1).txt

来自「C_C++使用技巧全集」· 文本 代码 · 共 139 行

TXT
139
字号
作者:wgenry
email: wgenry@sina.com
日期:2000-8-24 8:19:29
如果你使用ADSI的话
大概应该是这样的
Set obja = GetObject path 
'path 的格式为WINNT://<Domain>/Users/<User>,如果用户在Users下的话
obja.ChangePassword OldPwd,newPwd

如果用ISAPI或者ASP Component
STDMETHODIMP CPwdMgr::ChangePassword(BSTR Domain, BSTR UserName, BSTR OldPwd, BSTR NewPwd, VARIANT_BOOL *RetVal)
{
    HRESULT			hr;
	LPWSTR          wUserName;
    LPWSTR          wComputerName = NULL; // default to local machine
	LPWSTR          wDomain;
    LPWSTR          wOldPassword;
    LPWSTR          wNewPassword;
    USER_INFO_1003  pi1003;
    NET_API_STATUS  nas;
	
	_bstr_t bstrDomain,bstrUser,bstrOldPwd,bstrNewPwd;
	bstrDomain = Domain;
	bstrUser = UserName;
	bstrOldPwd = OldPwd;
	bstrNewPwd = NewPwd;
	
	wDomain = (wchar_t*)bstrDomain;
    wUserName = (wchar_t*)bstrUser;
	wOldPassword = (wchar_t*)bstrOldPwd;
    wNewPassword = (wchar_t*)bstrNewPwd;

	nas = NetGetDCName(
        NULL,
        wDomain,
        (LPBYTE *)&wComputerName
        );

    if(nas != NERR_Success)
	{
        if (nas == NERR_DCNotFound)
			AtlReportError(CLSID_PwdMgr,"Could not find the domain controller for the domain",IID_IPwdMgr,nas);
		if (nas == ERROR_INVALID_NAME)
			AtlReportError(CLSID_PwdMgr,"The name could not be found",IID_IPwdMgr,nas);
		*RetVal = VARIANT_FALSE;
		return E_FAIL;
    }

    if(wOldPassword == NULL)
	{
        pi1003.usri1003_password = wNewPassword;

        nas = NetUserSetInfo(
                wComputerName,  // computer name
                wUserName,      // username
                1003,           // info level
                (LPBYTE)&pi1003,     // new info
                NULL
                );
		if (nas != NERR_Success)
		{
			switch(nas)
			{
			case ERROR_ACCESS_DENIED:
				AtlReportError(CLSID_PwdMgr,"The user does not have access to the requested information",IID_IPwdMgr,nas);
				break;
			case ERROR_INVALID_PARAMETER:
				AtlReportError(CLSID_PwdMgr,"One of the function parameters is invalid. For more information, see the following Remarks section",IID_IPwdMgr,nas);
				break;
			case NERR_InvalidComputer:
				AtlReportError(CLSID_PwdMgr,"The computer name is invalid",IID_IPwdMgr,nas);
				break;
			case NERR_NotPrimary:
				AtlReportError(CLSID_PwdMgr,"The operation is allowed only on the primary domain controller of the domain",IID_IPwdMgr,nas);
				break;
			case NERR_SpeGroupOp:
				AtlReportError(CLSID_PwdMgr,"The operation is not allowed on specified special groups, which are user groups, admin groups, local groups, or guest groups",IID_IPwdMgr,nas);
				break;
			case NERR_LastAdmin:
				AtlReportError(CLSID_PwdMgr,"The operation is not allowed on the last administrative account",IID_IPwdMgr,nas);
				break;
			case NERR_BadPassword:
				AtlReportError(CLSID_PwdMgr,"The share name or password is invalid",IID_IPwdMgr,nas);
				break;
			case NERR_PasswordTooShort:
				AtlReportError(CLSID_PwdMgr,"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.)",IID_IPwdMgr,nas);
				break;
			case NERR_UserNotFound:
				AtlReportError(CLSID_PwdMgr,"The user name could not be found",IID_IPwdMgr,nas);
				break;
			}
		}
    }
	else
	{
        nas = NetUserChangePassword(
                wComputerName,
                wUserName,
                wOldPassword,
                wNewPassword
                );
		if (nas != NERR_Success)
		{
			switch(nas)
			{
			case ERROR_ACCESS_DENIED:
				AtlReportError(CLSID_PwdMgr,"The user does not have access to the requested information",IID_IPwdMgr,nas);
				break;
			case ERROR_INVALID_PASSWORD :
				AtlReportError(CLSID_PwdMgr,"The user has entered an invalid password",IID_IPwdMgr,nas);
				break;
			case NERR_InvalidComputer:
				AtlReportError(CLSID_PwdMgr,"The computer name is invalid",IID_IPwdMgr,nas);
				break;
			case NERR_NotPrimary:
				AtlReportError(CLSID_PwdMgr,"The operation is allowed only on the primary domain controller of the domain",IID_IPwdMgr,nas);
				break;
			case NERR_UserNotFound:
				AtlReportError(CLSID_PwdMgr,"The user name could not be found",IID_IPwdMgr,nas);
				break;
			case NERR_PasswordTooShort:
				AtlReportError(CLSID_PwdMgr,"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.)",IID_IPwdMgr,nas);
				break;
			}
		}
    }

    NetApiBufferFree(wComputerName);

    if(nas != NERR_Success) {
        *RetVal = VARIANT_FALSE;
        return E_FAIL;
    }

    *RetVal = VARIANT_TRUE;
	return S_OK;


}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?