Home > OS >  BiometricPrompt.Authenticate() doesn't wait for user to authenticate
BiometricPrompt.Authenticate() doesn't wait for user to authenticate

Time:09-17

I'm trying to implement user authentication via Fingerprint for my Xamarin.Forms mobile App, and for this I'm using the Android.Hardware.Biometrics library. This is the code i'm using to instantiate and show the prompt:

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;
}

And this is the BiometrcAuthenticationCallback class:

class BiometricAuthenticationCallback : BiometricPrompt.AuthenticationCallback
{
    public bool authSuccessful = false;

    public override void OnAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result)
    {
        authSuccessful = true;
    }
}

Ideally, BiometricPrompt.Authenticate() would stop code execution until the user interacts with the fingerprint scanner so you can read the results from authCallback, right? However, this is not the case... the app does prompt the user to authenticate using his fingerprint, but the app just continues to run, so it immediately returns "false" on the next line...

What am I doing wrong? Am I supposed to stop code execution myself until the user has confirmed their identity? All the code examples i've seen so far have not done this...

CodePudding user response:

I think the logic of your function public bool Authenticate() doesn't make sense.

After you called the following code,why would you call the return function immediately(return authCallback.authSuccessful;)? Since we haven't authenticate using the fingerprint at this moment,so code return authCallback.authSuccessful; will return false immediately.

BiometricAuthenticationCallback authCallback = new BiometricAuthenticationCallback();
biometricPrompt.Authenticate(new CryptoObjectHelper().BuildCryptoObject(), new Android.OS.CancellationSignal(), Android.App.Application.Context.MainExecutor, authCallback);

For this, you can refer the following code:

https://gist.github.com/justintoth/49484bb0c3a0494666442f3e4ea014c0

  • Related