UserConsentVerifier 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
檢查驗證裝置的可用性 (,例如 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
- 繼承
- 屬性
Windows 需求
裝置系列 |
Windows 10 (已於 10.0.10240.0 引進)
|
API contract |
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)
|
範例
使用 C 的桌面應用程式#
針對傳統型應用程式,您不需要呼叫 UserConsentVerifier.RequestVerificationAsync 方法,您必須:
- 首先 , (HWND) 擷取視窗控制碼 。 該主題包含 Windows UI 程式庫 (WinUI) 3、Windows Presentation Foundation (WPF) 和 Windows Forms (WinForms) 的程式碼範例。 將該程式碼插入下列程式代碼清單中。
- 然後呼叫Windows.Security.Credentials.UI.UserConsentVerifierInterop C# Interop類別的RequestVerificationForWindowAsync方法。 如需 C# Interop 類別的詳細資訊,請參閱 從 .NET 應用程式呼叫 Interop API。 另請參閱 顯示相依于 CoreWindow 的 WinRT UI 物件。
private async System.Threading.Tasks.Task<string> RequestConsent(string userMessage)
{
string returnMessage;
if (String.IsNullOrEmpty(userMessage))
{
userMessage = "Please provide fingerprint verification.";
}
try
{
// Retrieve the window handle by passing a reference to the WinUI 3 window object
var hwnd = ...
// Request the logged on user's consent via fingerprint swipe using the interop interface
var consentResult = await Windows.Security.Credentials.UI.UserConsentVerifierInterop.RequestVerificationForWindowAsync(hwnd, userMessage);
switch (consentResult)
{
case Windows.Security.Credentials.UI.UserConsentVerificationResult.Verified:
returnMessage = "Fingerprint verified.";
break;
case Windows.Security.Credentials.UI.UserConsentVerificationResult.DeviceBusy:
returnMessage = "Biometric device is busy.";
break;
case Windows.Security.Credentials.UI.UserConsentVerificationResult.DeviceNotPresent:
returnMessage = "No biometric device found.";
break;
case Windows.Security.Credentials.UI.UserConsentVerificationResult.DisabledByPolicy:
returnMessage = "Biometric verification is disabled by policy.";
break;
case Windows.Security.Credentials.UI.UserConsentVerificationResult.NotConfiguredForUser:
returnMessage = "The user has no fingerprints registered. Please add a fingerprint to the " +
"fingerprint database and try again.";
break;
case Windows.Security.Credentials.UI.UserConsentVerificationResult.RetriesExhausted:
returnMessage = "There have been too many failed attempts. Fingerprint authentication canceled.";
break;
case Windows.Security.Credentials.UI.UserConsentVerificationResult.Canceled:
returnMessage = "Fingerprint authentication canceled.";
break;
default:
returnMessage = "Fingerprint authentication is currently unavailable.";
break;
}
}
catch (Exception ex)
{
returnMessage = "Fingerprint authentication failed: " + ex.ToString();
}
return returnMessage;
}
使用 C 通用 Windows 平臺 (UWP) 應用程式#
此程式碼範例適用于通用 Windows 平臺 (UWP) app。 它會顯示指紋驗證的要求,後面接著傳回描述結果的訊息。 程式碼會呼叫適用于 UWP 應用程式的 UserConsentVerifier.RequestVerificationAsync 方法。
private async System.Threading.Tasks.Task<string> RequestConsent(string userMessage)
{
string returnMessage;
if (String.IsNullOrEmpty(userMessage))
{
userMessage = "Please provide fingerprint verification.";
}
try
{
// Request the logged on user's consent via fingerprint swipe.
var consentResult = await Windows.Security.Credentials.UI.UserConsentVerifier.RequestVerificationAsync(userMessage);
switch (consentResult)
{
case Windows.Security.Credentials.UI.UserConsentVerificationResult.Verified:
returnMessage = "Fingerprint verified.";
break;
case Windows.Security.Credentials.UI.UserConsentVerificationResult.DeviceBusy:
returnMessage = "Biometric device is busy.";
break;
case Windows.Security.Credentials.UI.UserConsentVerificationResult.DeviceNotPresent:
returnMessage = "No biometric device found.";
break;
case Windows.Security.Credentials.UI.UserConsentVerificationResult.DisabledByPolicy:
returnMessage = "Biometric verification is disabled by policy.";
break;
case Windows.Security.Credentials.UI.UserConsentVerificationResult.NotConfiguredForUser:
returnMessage = "The user has no fingerprints registered. Please add a fingerprint to the " +
"fingerprint database and try again.";
break;
case Windows.Security.Credentials.UI.UserConsentVerificationResult.RetriesExhausted:
returnMessage = "There have been too many failed attempts. Fingerprint authentication canceled.";
break;
case Windows.Security.Credentials.UI.UserConsentVerificationResult.Canceled:
returnMessage = "Fingerprint authentication canceled.";
break;
default:
returnMessage = "Fingerprint authentication is currently unavailable.";
break;
}
}
catch (Exception ex)
{
returnMessage = "Fingerprint authentication failed: " + ex.ToString();
}
return returnMessage;
}
備註
您可以使用 UserConsentVerifier 來增強應用程式的安全性,方法是在使用者需要同意特定動作時加入驗證要求。 例如,您可以先要求指紋驗證,再授權應用程式內購買或存取受限制的資源。 您可以使用 UserConsentVerifier ,使用 CheckAvailabilityAsync 方法來判斷目前電腦是否支援指紋驗證,然後使用 RequestVerificationAsync 方法向指紋掃描要求使用者同意。
方法
CheckAvailabilityAsync() |
檢查是否提供驗證器裝置,例如 Microsoft Passport PIN、Windows Hello或指紋辨識器。 |
RequestVerificationAsync(String) |
使用 Microsoft Passport PIN、Windows Hello或指紋辨識器等裝置執行驗證。 此 API 適用于 通用 Windows 平臺 (UWP) 應用程式。 傳統型應用程式的替代 API 會在UserConsentVerifier 類別的範例中說明。 |
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應