UserConsentVerifier Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Memeriksa ketersediaan perangkat verifikasi (seperti PIN Paspor Microsoft, Windows Hello biometrik, atau pembaca sidik jari), dan melakukan verifikasi.
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
- Warisan
- Atribut
Persyaratan Windows
Rangkaian perangkat |
Windows 10 (diperkenalkan dalam 10.0.10240.0)
|
API contract |
Windows.Foundation.UniversalApiContract (diperkenalkan dalam v1.0)
|
Contoh
Aplikasi desktop menggunakan C#
Untuk aplikasi desktop, alih-alih memanggil metode UserConsentVerifier.RequestVerificationAsync , Anda harus:
- Pertama Ambil handel jendela (HWND). Topik tersebut berisi contoh kode untuk Windows UI Library (WinUI) 3, Windows Presentation Foundation (WPF), dan Formulir Windows (WinForms). Colokkan kode tersebut ke dalam daftar kode di bawah ini.
- Kemudian panggil metode RequestVerificationForWindowAsync dari kelas interop Windows.Security.Credentials.UI.UserConsentVerifierInterop C#. Untuk informasi selengkapnya tentang kelas interop C#, lihat Memanggil API interop dari aplikasi .NET. Lihat juga Menampilkan objek UI WinRT yang bergantung pada CoreWindow.
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;
}
aplikasi Platform Windows Universal (UWP) menggunakan C#
Contoh kode ini untuk aplikasi Platform Windows Universal (UWP). Ini menunjukkan permintaan verifikasi sidik jari diikuti dengan mengembalikan pesan yang menjelaskan hasilnya. Kode ini memanggil metode UserConsentVerifier.RequestVerificationAsync , yang sesuai untuk aplikasi UWP.
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;
}
Aplikasi desktop menggunakan C++/WinRT
Untuk aplikasi desktop, alih-alih memanggil metode UserConsentVerifier.RequestVerificationAsync , Anda harus:
- Pertama, dapatkan handel ke jendela yang akan meminta verifikasi. Lihat Mengambil handel jendela (HWND) untuk detail tentang cara melakukan ini untuk Pustaka Windows UI (WinUI) 3.
- Dapatkan pabrik aktivasi untuk objek Windows.Security.Credentials.UI.UserConsentVerifier .
- Panggil metode RequestVerificationForWindowAsync dari antarmuka interop IUserConsentVerifierInterop .
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;
}
Keterangan
Anda dapat menggunakan UserConsentVerifier untuk meningkatkan keamanan aplikasi Anda dengan menyertakan permintaan verifikasi setiap kali pengguna diharuskan menyetujui tindakan tertentu. Misalnya, Anda dapat memerlukan autentikasi sidik jari sebelum mengotorisasi pembelian dalam aplikasi atau akses ke sumber daya terbatas. Anda dapat menggunakan UserConsentVerifier untuk menentukan apakah autentikasi sidik jari didukung untuk komputer saat ini dengan menggunakan metode CheckAvailabilityAsync , lalu meminta persetujuan pengguna dari pemindaian sidik jari dengan menggunakan metode RequestVerificationAsync .
Metode
CheckAvailabilityAsync() |
Memeriksa untuk melihat apakah perangkat autentikasi, seperti PIN Paspor Microsoft, Windows Hello, atau pembaca sidik jari, tersedia. |
RequestVerificationAsync(String) |
Melakukan verifikasi menggunakan perangkat autentikasi seperti PIN Paspor Microsoft, Windows Hello, atau pembaca sidik jari. API ini untuk aplikasi Platform Windows Universal (UWP). API alternatif yang digunakan untuk aplikasi desktop dijelaskan dalam Contoh di kelas UserConsentVerifier. |