Partager via


Verrouillage de compte (fournisseur WinNT)

Lorsque le nombre de tentatives d’ouverture de session ayant échoué est dépassé, le compte d’utilisateur est verrouillé pendant le nombre de minutes spécifié par l’attribut lockoutDuration . La propriété IADsUser.IsAccountLocked semble être la propriété à utiliser pour lire et modifier l’état de verrouillage d’un compte d’utilisateur, mais le fournisseur WinNT ADSI a des restrictions qui limitent l’utilisation de la propriété IsAccountLocked .

Réinitialisation de l’état du verrouillage de compte

Lorsque vous utilisez le fournisseur WinNT, la propriété IsAccountLocked ne peut être définie que sur FALSE, ce qui déverrouille le compte. Toute tentative de définition de la propriété IsAccountLocked sur TRUE échoue. Seul le système peut verrouiller un compte.

L’exemple de code suivant montre comment utiliser Visual Basic avec ADSI pour déverrouiller un compte d’utilisateur.

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

L’exemple de code suivant montre comment utiliser C++ avec ADSI pour déverrouiller un compte d’utilisateur.

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;
}

Lecture de l’état de verrouillage de compte

Avec le fournisseur WinNT, la propriété IsAccountLocked peut être utilisée pour déterminer si un compte est verrouillé. Si un compte est verrouillé, la propriété IsAccountLocked contient TRUE. Si un compte n’est pas verrouillé, la propriété IsAccountLocked contient FALSE.

L’exemple de code suivant montre comment utiliser Visual Basic avec ADSI pour déterminer si un compte est verrouillé.

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

L’exemple de code suivant montre comment utiliser C++ avec ADSI pour déterminer si un compte est verrouillé.

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;
}