我试图通过指纹为我的Xamarin实现用户身份验证。表单移动应用程序,为此我使用android . hardware . biometics库。这是我用来实例化和显示提示符的代码:
public bool Authenticate()
{
// Create biometric prompt
biometricPrompt = new BiometricPrompt.Builder(Android.App.Application.Context)
.SetTitle("Authenticate")
//this is most definitely wrong but just ignore this
.SetNegativeButton("Cancel", Android.App.Application.Context.MainExecutor, new Android.App.DatePickerDialog(Android.App.Application.Context))
.Build();
BiometricAuthenticationCallback authCallback = new BiometricAuthenticationCallback();
biometricPrompt.Authenticate(new CryptoObjectHelper().BuildCryptoObject(), new Android.OS.CancellationSignal(), Android.App.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";下一行…
我做错了什么?我应该自己停止代码执行,直到用户确认他们的身份吗?到目前为止,我看到的所有代码示例都没有这样做…
我认为你的函数public bool Authenticate()
的逻辑没有意义。
调用以下代码后,为什么要立即调用返回函数(return authCallback.authSuccessful;
)?由于我们目前没有使用指纹进行身份验证,所以代码return authCallback.authSuccessful;
将立即返回false
。
BiometricAuthenticationCallback authCallback = new BiometricAuthenticationCallback();
biometricPrompt.Authenticate(new CryptoObjectHelper().BuildCryptoObject(), new Android.OS.CancellationSignal(), Android.App.Application.Context.MainExecutor, authCallback);
对此,可以参考以下代码:
https://gist.github.com/justintoth/49484bb0c3a0494666442f3e4ea014c0