修改用户无法更改密码(WinNT 提供程序)
用户更改自己的密码的能力是一种可以授予或拒绝的权限。 要拒绝此此权限,请在用户对象的 userFlags 属性中添加 ADS_UF_PASSWD_CANT_CHANGE 标记。 要授予此权限,请删除用户对象 userFlags 属性中的 ADS_UF_PASSWD_CANT_CHANGE 标记。
示例代码
下面的代码示例显示了如何更改用户对象 userFlags 属性中的 ADS_UF_PASSWD_CANT_CHANGE 标志。
Const ADS_UF_PASSWD_CANT_CHANGE = &H40
Sub SetUserCannotChangePassword(strDomain As String, strUser As String, strUserCred As String, strPassword As String, fUserCannotChangePassword As Boolean)
Dim oUser As IADs
strPath = "WinNT://" + strDomain + "/" + strUser
If "" <> strUserCred Then
Dim dso As IADsOpenDSObject
' Bind to the group with the specified user name and password.
Set dso = GetObject("WinNT:")
Set oUser = dso.OpenDSObject(strPath, strUserCred, strPassword, 1)
Else
' Bind to the group with the current credentials.
Set oUser = GetObject(strPath)
End If
lUserFlags = oUser.Get("userFlags")
If fUserCannotChangePassword Then
lUserFlags = lUserFlags Or ADS_UF_PASSWD_CANT_CHANGE
Else
lUserFlags = lUserFlags And Not ADS_UF_PASSWD_CANT_CHANGE
End If
' Modify the userFlags property.
oUser.Put "userFlags", lUserFlags
' Commit the changes to the server.
oUser.SetInfo
End Sub
下面的代码示例显示了如何更改用户对象 userFlags 属性中的 ADS_UF_PASSWD_CANT_CHANGE 标志。
//***************************************************************************
// SetUserCannotChangePassword()
//***************************************************************************
HRESULT SetUserCannotChangePassword(LPCWSTR pwszDomain,
LPCWSTR pwszUser,
LPCWSTR pwszUserCred,
LPCWSTR pwszPassword,
BOOL fCannotChangePassword)
{
if(NULL == pwszDomain ||
NULL == pwszUser)
{
return E_INVALIDARG;
}
HRESULT hr;
IADs *pads;
CComBSTR sbstrADsPath = L"WinNT://";
sbstrADsPath += pwszDomain;
sbstrADsPath += "/";
sbstrADsPath += pwszUser;
hr = ADsOpenObject( sbstrADsPath,
pwszUserCred,
pwszPassword,
ADS_SECURE_AUTHENTICATION,
IID_IADs,
(void**)&pads);
if(SUCCEEDED(hr))
{
CComBSTR sbstrPropName = "userFlags";
CComVariant svar;
hr = pads->Get(sbstrPropName, &svar);
if(SUCCEEDED(hr))
{
if(fCannotChangePassword)
{
svar.lVal |= ADS_UF_PASSWD_CANT_CHANGE;
}
else
{
svar.lVal &= ~ADS_UF_PASSWD_CANT_CHANGE;
}
// Perform the change.
hr = pads->Put(sbstrPropName, svar);
// Commit the change.
hr = pads->SetInfo();
}
pads->Release();
}
return hr;
}