變更使用者無法變更密碼 (WinNT 提供者)
用戶變更自己的密碼的能力是可以授與或拒絕的許可權。 若要拒絕此許可權,請將 ADS_UF_PASSWD_CANT_CHANGE 旗標新增至 用戶物件的userFlags 屬性。 若要授與此許可權,請從用戶物件的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;
}