共用方式為


帳戶鎖定 (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;
}