我正試圖為我的Xamarin.Forms移動應用程式通過指紋實作用戶認證,為此我使用了Android.Hardware.Biometrics庫。這是我用來實體化和顯示提示的代碼:
public bool Authenticate()
{
//>創建生物識別提示
biometricPrompt = new BiometricPrompt.Builder(Android.Application.Context)
.SetTitle("Authenticate")
//this is most definitely wrong but just ignore this。
.SetNegativeButton("Cancel", Android.App.Application.Context.MainExecutor, newAndroid.App.DatePickerDialog(Android.Application.Context))
.Build()。
BiometricAuthenticationCallback authCallback = new BiometricAuthenticationCallback()。
biometricPrompt.Authenticate(new CryptoObjectHelper().BuildCryptoObject(), new Android.OS.CancellationSignal(), Android.Application.Context.MainExecutor, authCallback) 。
return authCallback.authSuccessful;
而這是BiometrcAuthenticationCallback類:
class BiometricAuthenticationCallback : BiometricPrompt.AuthenticationCallback
{
public bool authSuccessful = false;
public override void OnAuthenticationSucceeded(BiometricPrompt. AuthenticationResult result)。
{
authSuccessful = true;
}
}
通常情況下,BiometricPrompt.Authenticate()會停止代碼的執行,直到用戶與指紋掃描儀進行互動,這樣你就可以從authCallback讀取結果,對嗎?然而,情況并非如此......應用程式確實提示用戶使用其指紋進行認證,但應用程式只是繼續運行,因此它在下一行立即回傳 "false"......
我在做什么?
我做錯了什么?我是否應該自己停止代碼的執行,直到用戶確認了他們的身份?
uj5u.com熱心網友回復:
我認為你的函式public bool Authenticate()的邏輯沒有意義。
在你呼叫下面的代碼后,為什么要立即呼叫回傳函式(return authCallback.authSuccessful;)? 因為我們此刻還沒有使用指紋認證,所以代碼return authCallback.authSuccessful;將立即回傳false。
BiometricAuthenticationCallback authCallback = new BiometricAuthenticationCallback()。
biometricPrompt.Authenticate(new CryptoObjectHelper().BuildCryptoObject(), new Android.OS.CancellationSignal(), Android.Application.Context.MainExecutor, authCallback) 。
為此,你可以參考以下代碼:
https://gist.github.com/justintoth/49484bb0c3a0494666442f3e4ea014c0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/315499.html
標籤:
上一篇:結構內的結構:指向或不指向?
