【发布时间】:2021-09-10 09:07:29
【问题描述】:
我正在尝试通过指纹为我的 Xamarin.Forms 移动应用程序实现用户身份验证,为此我正在使用 Android.Hardware.Biometrics 库。这是我用来实例化和显示提示的代码:
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”......
我做错了什么?在用户确认其身份之前,我是否应该自己停止代码执行?到目前为止我看到的所有代码示例都没有这样做...
【问题讨论】:
标签: c# authentication xamarin.android fingerprint