次の方法で共有


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) アプリ用です。 指紋認証の要求の後に、結果を説明するメッセージを返すメッセージが表示されます。 このコードは、UWP アプリに適した 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 相互運用インターフェイスの 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) アプリ用です。 デスクトップ アプリに使用する代替 API については、「UserConsentVerifier クラス」を参照してください。

適用対象

こちらもご覧ください