Steve Parry
Steve Parry

Reputation: 428

MAUI prompt for biometric/Pin authentication when leaving the app and then going back to it

I'm using the nuget package Plugin.Fingerprint in my MAUI app to enable the use of the device authentication. I have the code working on one of my forms. But now Im trying to apply it so when I resume the use of the app it will prompt for a security check (if enabled).

In my MauiProgram.cs I Have

builder.ConfigureLifecycleEvents(AppLifecycle => {
#if ANDROID
     AppLifecycle.AddAndroid(android => android
        .OnRestart((activity) =>
        
            LocalDeviceAuthPrompt()               
        ));
#endif
    });

and a private method

private static bool LocalDeviceAuthPrompt()
{
    var isAvailable = CrossFingerprint.Current.IsAvailableAsync(true).Result;

    if (isAvailable)
    {
        var request = new AuthenticationRequestConfiguration
        ("Login using biometrics", "Confirm login with your biometrics");
        request.AllowAlternativeAuthentication = true;
        var result = CrossFingerprint.Current.AuthenticateAsync(request).Result;

        if (result.Authenticated)
        {
            return true;
        }
        
        return false;
    }

    return true;
}

I know theres plenty wrong with it, for a start async is needed but the AppLifecycle implementation doesnt support it. It also doesnt work. the event fires but I never get a security prompt. Does anyone have any advice/recommendations

UPDATE1

So I removed the above code and added it to the app.xaml.cs file as follows

protected override async void OnResume()
    {
        var isAvailable = await CrossFingerprint.Current.IsAvailableAsync(true);

        if (isAvailable)
        {
            var request = new AuthenticationRequestConfiguration
            ("Login using biometrics", "Confirm login with your biometrics");
            request.AllowAlternativeAuthentication = true;
            var result = await CrossFingerprint.Current.AuthenticateAsync(request);

            Console.Write(result);

        }
    }

I do get to decorate the method with async but it has the exact same issue. It doesn't go any further than line "CrossFingerprint.Current.AuthenticateAsync(request)" . No error it just hangs

Upvotes: 2

Views: 1625

Answers (1)

Liyun Zhang - MSFT
Liyun Zhang - MSFT

Reputation: 14509

I have created a new .net 6.0 maui project to test your code and it worked when I put the code in the MainActivity's OnResume method.

Add permission in the AndroidManifest.xml:

<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<uses-permission android:name="android.permission.USE_BIOMETRIC" /> 

And my MainActivity:

public class MainActivity : MauiAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        CrossFingerprint.SetCurrentActivityResolver(() => this);
      //bool a = ActivityCompat.CheckSelfPermission(this, Manifest.Permission.UseBiometric) == Permission.Granted;
      // check the permission at runtime
    }
    protected override async void OnResume()
    {
        base.OnResume();
        var isAvailable = await CrossFingerprint.Current.IsAvailableAsync(true);
        if (isAvailable)
        {
            var request = new AuthenticationRequestConfiguration
            ("Login using biometrics", "Confirm login with your biometrics");
            request.AllowAlternativeAuthentication = true;
            var result = await CrossFingerprint.Current.AuthenticateAsync(request);
            Console.Write(result);
        }
    }
}

And the result image:

enter image description here

Upvotes: 1

Related Questions