共用方式為


UserConsentVerifier 類別

定義

檢查驗證裝置的可用性 (,例如 Microsoft Passport PIN、Windows Hello 生物特徵辨識或指紋讀取器) ,並執行驗證。

public ref class UserConsentVerifier abstract sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class UserConsentVerifier final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public static class UserConsentVerifier
Public Class UserConsentVerifier
繼承
Object Platform::Object IInspectable UserConsentVerifier
屬性

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

範例

使用 C 的桌面應用程式#

針對傳統型應用程式,您不需要呼叫 UserConsentVerifier.RequestVerificationAsync 方法,而是需要:

private async System.Threading.Tasks.Task<string> RequestConsent(string userMessage)
{
    string returnMessage;

    // Retrieve the window handle of the current WinUI 3 window.
    var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);

    // Use the interop interface to request the logged on user's consent via device authentication
    var consentResult = await Windows.Security.Credentials.UI.UserConsentVerifierInterop.RequestVerificationForWindowAsync(hwnd, userMessage);

    switch (consentResult)
    {
        case Windows.Security.Credentials.UI.UserConsentVerificationResult.Verified:
            returnMessage = "User verified.";
            break;
        case Windows.Security.Credentials.UI.UserConsentVerificationResult.DeviceBusy:
            returnMessage = "Authentication device is busy.";
            break;
        case Windows.Security.Credentials.UI.UserConsentVerificationResult.DeviceNotPresent:
            returnMessage = "No authentication device found.";
            break;
        case Windows.Security.Credentials.UI.UserConsentVerificationResult.DisabledByPolicy:
            returnMessage = "Authentication device verification is disabled by policy.";
            break;
        case Windows.Security.Credentials.UI.UserConsentVerificationResult.NotConfiguredForUser:
            returnMessage = "Please go to Account Settings to set up PIN or other advanced authentication.";
            break;
        case Windows.Security.Credentials.UI.UserConsentVerificationResult.RetriesExhausted:
            returnMessage = "There have been too many failed attempts. Device authentication canceled.";
            break;
        case Windows.Security.Credentials.UI.UserConsentVerificationResult.Canceled:
            returnMessage = "Device authentication canceled.";
            break;
        default:
            returnMessage = "Authentication device is currently unavailable.";
            break;
    }

    return returnMessage;
}

使用 C 通用 Windows 平台 (UWP) 應用程式#

此程式代碼範例適用於 通用 Windows 平台 (UWP) app。 它會顯示指紋驗證的要求,後面接著傳回描述結果的訊息。 程序代碼會呼叫適用於UWP app的 UserConsentVerifier.RequestVerificationAsync 方法。

private async System.Threading.Tasks.Task<string> RequestConsent(string userMessage)
{
    string returnMessage;

    // Request the logged on user's consent via authentication device.
    var consentResult = await Windows.Security.Credentials.UI.UserConsentVerifier.RequestVerificationAsync(userMessage);

    switch (consentResult)
    {
        case Windows.Security.Credentials.UI.UserConsentVerificationResult.Verified:
            returnMessage = "User verified.";
            break;
        case Windows.Security.Credentials.UI.UserConsentVerificationResult.DeviceBusy:
            returnMessage = "Authentication device is busy.";
            break;
        case Windows.Security.Credentials.UI.UserConsentVerificationResult.DeviceNotPresent:
            returnMessage = "No authentication device found.";
            break;
        case Windows.Security.Credentials.UI.UserConsentVerificationResult.DisabledByPolicy:
            returnMessage = "Authentication device verification is disabled by policy.";
            break;
        case Windows.Security.Credentials.UI.UserConsentVerificationResult.NotConfiguredForUser:
            returnMessage = "Please go to Account Settings to set up PIN or other advanced authentication.";
            break;
        case Windows.Security.Credentials.UI.UserConsentVerificationResult.RetriesExhausted:
            returnMessage = "There have been too many failed attempts. Device authentication canceled.";
            break;
        case Windows.Security.Credentials.UI.UserConsentVerificationResult.Canceled:
            returnMessage = "Device authentication canceled.";
            break;
        default:
            returnMessage = "Authentication device is currently unavailable.";
            break;
    }
    return returnMessage;
}

使用 C++/WinRT 的桌面應用程式

針對傳統型應用程式,您不需要呼叫 UserConsentVerifier.RequestVerificationAsync 方法,而是需要:

  • 首先取得將要求驗證之視窗的句柄。 如需如何針對 Windows UI 連結庫 (WinUI) 3 執行這項操作的詳細資訊,請參閱 擷取 (HWND) 視窗句柄
  • 取得 Windows.Security.Credentials.UI.UserConsentVerifier 對象的啟用處理站。
  • 呼叫 IUserConsentVerifierInterop Interop 介面的 RequestVerificationForWindowAsync 方法。
winrt::Windows::Foundation::IAsyncOperation<winrt::hstring> MainWindow::RequestConsent(winrt::hstring userMessage)
{
    auto lifetime = get_strong();

    winrt::hstring returnMessage;

    // Retrieve the window handle of the current WinUI 3 window.
    HWND hwnd;
    winrt::check_hresult(m_inner->as<::IWindowNative>()->get_WindowHandle(&hwnd));

    // Use the interop interface to request the logged on user's consent via device authentication
    auto interop = winrt::get_activation_factory<winrt::Windows::Security::Credentials::UI::UserConsentVerifier, ::IUserConsentVerifierInterop>();
    auto consentResult =
        co_await winrt::capture<winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Security::Credentials::UI::UserConsentVerificationResult>>(
            interop, &::IUserConsentVerifierInterop::RequestVerificationForWindowAsync, hwnd, reinterpret_cast<HSTRING>(winrt::get_abi(userMessage))));

    switch (consentResult)
    {
        case winrt::Windows::Security::Credentials::UI::UserConsentVerificationResult::Verified:
            returnMessage = L"User verified.";
            break;
        case winrt::Windows::Security::Credentials::UI::UserConsentVerificationResult::DeviceBusy:
            returnMessage = L"Authentication device is busy.";
            break;
        case winrt::Windows::Security::Credentials::UI::UserConsentVerificationResult::DeviceNotPresent:
            returnMessage = L"No authentication device found.";
            break;
        case winrt::Windows::Security::Credentials::UI::UserConsentVerificationResult::DisabledByPolicy:
            returnMessage = L"Authentication device verification is disabled by policy.";
            break;
        case winrt::Windows::Security::Credentials::UI::UserConsentVerificationResult::NotConfiguredForUser:
            returnMessage = L"Please go to Account Settings to set up PIN or other advanced authentication.";
            break;
        case winrt::Windows::Security::Credentials::UI::UserConsentVerificationResult::RetriesExhausted:
            returnMessage = L"There have been too many failed attempts. Device authentication canceled.";
            break;
        case winrt::Windows::Security::Credentials::UI::UserConsentVerificationResult::Canceled:
            returnMessage = L"Device authentication canceled.";
            break;
        default:
            returnMessage = L"Authentication device is currently unavailable.";
            break;
    }

    co_return returnMessage;
}

備註

您可以使用 UserConsentVerifier 來增強應用程式的安全性,方法是在使用者需要同意特定動作時包含驗證要求。 例如,您可以先要求指紋驗證,再授權應用程式內購買或存取受限制的資源。 您可以使用 UserConsentVerifier ,使用 CheckAvailabilityAsync 方法來判斷目前電腦是否支援指紋驗證,然後使用 RequestVerificationAsync 方法向指紋掃描要求使用者同意。

方法

CheckAvailabilityAsync()

檢查是否有可用的驗證裝置,例如 Microsoft Passport PIN、Windows Hello 或指紋讀取器。

RequestVerificationAsync(String)

使用驗證裝置執行驗證,例如 Microsoft Passport PIN、Windows Hello 或指紋讀取器。 此 API 適用於 通用 Windows 平台 (UWP) 應用程式。 在 UserConsentVerifier 類別範例中,會說明用於傳統型應用程式的替代 API。

適用於

另請參閱