指紋生物識別技術
本文說明如何將指紋生物識別技術新增至 Windows 應用程式,包括在使用者必須同意特定動作時請求指紋驗證,以提高應用程式的安全性。 例如,在授權應用程式內購買之前,您可能需要指紋驗證,或存取受限制的資源。 指紋驗證是使用 Windows.Security.Credentials.UI 命名空間中的 UserConsentVerifier 類別所管理。
指紋生物識別 Windows 執行階段 (WinRT) API 是 Windows 軟體開發工具包 (SDK) 的一部分。 這些 API 已建立以用於 通用 Windows 平台 (UWP) app,但它們也可以在 WinUI 應用程式或封裝的桌面應用程式中使用,包括 WPF 和 Windows Forms。 如需在 Windows 傳統型應用程式中使用 WinRT API 的詳細資訊,請參閱在傳統型應用程式中呼叫 Windows 執行階段 API。
檢查裝置是否有指紋讀取器
若要了解裝置是否有指紋讀取器,請呼叫 UserConsentVerifier.CheckAvailabilityAsync。 即使裝置支援指紋身分驗證,您的應用程式仍應在「設定」中為使用者提供啟用或停用它的選項。
public async System.Threading.Tasks.Task<string> CheckFingerprintAvailability()
{
string returnMessage = "";
try
{
// Check the availability of fingerprint authentication.
var ucvAvailability = await Windows.Security.Credentials.UI.UserConsentVerifier.CheckAvailabilityAsync();
switch (ucvAvailability)
{
case Windows.Security.Credentials.UI.UserConsentVerifierAvailability.Available:
returnMessage = "Fingerprint verification is available.";
break;
case Windows.Security.Credentials.UI.UserConsentVerifierAvailability.DeviceBusy:
returnMessage = "Biometric device is busy.";
break;
case Windows.Security.Credentials.UI.UserConsentVerifierAvailability.DeviceNotPresent:
returnMessage = "No biometric device found.";
break;
case Windows.Security.Credentials.UI.UserConsentVerifierAvailability.DisabledByPolicy:
returnMessage = "Biometric verification is disabled by policy.";
break;
case Windows.Security.Credentials.UI.UserConsentVerifierAvailability.NotConfiguredForUser:
returnMessage = "The user has no fingerprints registered. Please add a fingerprint to the " +
"fingerprint database and try again.";
break;
default:
returnMessage = "Fingerprints verification is currently unavailable.";
break;
}
}
catch (Exception ex)
{
returnMessage = $"Fingerprint authentication availability check failed: {ex.ToString()}";
}
return returnMessage;
}
請求同意並回傳結果
- 若要向指紋掃描要求使用者同意,請呼叫 UserConsentVerifier.RequestVerificationAsync 方法。 為了使指紋驗證起作用,使用者必須事先將指紋「簽名」新增至指紋資料庫。
- 當您呼叫 UserConsentVerifier.RequestVerificationAsync 時,使用者會看到要求指紋掃描的強制回應對話方塊。 您可以向 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;
}