帐户锁定(WinNT 提供程序)

当超过失败登录尝试的次数时,用户帐户将被锁定长达 lockoutDuration 属性指定的分钟数。 IADsUser.IsAccountLocked 属性似乎是用于读取和修改用户帐户锁定状态的属性,但是 WinNT ADSI 提供程序限制了 IsAccountLocked 属性的使用。

重置帐户锁定状态

使用 WinNT 提供程序时,IsAccountLocked 属性只能设置为 FALSE,从而解锁帐户。 尝试将 IsAccountLocked 属性设置为 TRUE 将失败。 只有系统才能锁定帐户。

以下代码示例显示如何将 Visual Basic 与 ADSI 配合使用来解锁用户帐户。

Public Sub UnlockAccount(AccountDN As String)
    Dim usr As IADsUser
    
    ' Bind to the user account.
    Set usr = GetObject("WinNT://" + AccountDN)
    
    ' Set the IsAccountLocked property to False
    usr.IsAccountLocked = False
    
    ' Flush the cached attributes to the server.
    usr.SetInfo
End Sub

以下代码示例显示如何将 C++ 与 ADSI 配合使用来解锁用户帐户。

HRESULT UnlockAccount(LPCWSTR pwszUserDN)
{
    if(!pwszUserDN)
    {
        return E_INVALIDARG;
    }

    // Build the ADsPath.
    CComBSTR sbstr = "WinNT://";
    sbstr += pwszUserDN;
    
    HRESULT hr;
    CComPtr<IADsUser> spADsUser;

    // Bind to the object.
    hr = ADsOpenObject(sbstr,
        NULL,
        NULL,
        ADS_SECURE_AUTHENTICATION,
        IID_IADsUser,
        (void**)&spADsUser);
    if(S_OK != hr)
    {
        return hr;
    }
    
    // Set the IsAccountLocked property to FALSE;
    hr = spADsUser->put_IsAccountLocked(VARIANT_FALSE);

    // Commit the changes to the server.
    hr = spADsUser->SetInfo();

    return hr;
}

读取帐户锁定状态

对于 WinNT 提供程序,可以使用 IsAccountLocked 属性来确定帐户是否被锁定。如果帐户被锁定,IsAccountLocked 属性将包含 TRUE。 如果未锁定帐户,IsAccountLocked 属性将包含 FALSE

以下代码示例显示如何将 Visual Basic 与 ADSI 配合使用来确定帐户是否已锁定。

Public Function IsAccountLocked(AccountDN As String) As Boolean
    Dim oUser As IADsUser
    
    ' Bind to the object.
    Set oUser = GetObject("WinNT://" + AccountDN)
    
    ' Get the IsAccountLocked property.
    IsAccountLocked = oUser.IsAccountLocked
End Function

以下代码示例显示如何将 C++ 与 ADSI 配合使用来确定帐户是否已锁定。

HRESULT IsAccountLocked(LPCWSTR pwszUserDN, BOOL *pfLocked)
{
    if(!pwszUserDN || !pfLocked)
    {
        return E_INVALIDARG;
    }

    *pfLocked = FALSE;

    // Build the ADsPath.
    CComBSTR sbstr = "WinNT://";
    sbstr += pwszUserDN;
    
    HRESULT hr;
    CComPtr<IADsUser> spADsUser;

    // Bind to the object.
    hr = ADsOpenObject(sbstr,
        NULL,
        NULL,
        ADS_SECURE_AUTHENTICATION,
        IID_IADsUser,
        (void**)&spADsUser);
    if(S_OK != hr)
    {
        return hr;
    }
    
    VARIANT_BOOL vfLocked;
    hr = spADsUser>get_IsAccountLocked(&vfLocked);
    if(S_OK != hr)
    {
        return hr;
    }

    *pfLocked = (vfLocked != 0);

    return hr;
}