Use biometric authentication in MAUI

xamarin fingerprint biometrics

In this new post, I am going to explain how to use biometric authentication with MAUI in your mobile or desktop applications.

Biometric authentication has become an increasingly integral part of mobile apps to ensure that the user is the rightful owner of the device that they’re using. Here’s how you can authenticate via Face ID (iOS) or fingerprint (Android / iOS) in your .NET MAUI app. Also, it is integrate in Windows 11: so, if you have Windows Hello enabled on your machine, the biometric authentication is available on desktops and laptops.

Add the package

First, create a new MAUI project in Visual Studio 2022 Preview.

Then, install the Plugin.Fingerprint NuGet package. You’ll need version 3.0.0-beta.1, which is currently in pre-release, so remember to check “Include prerelease”:

Find the NuGet package include prelease - How to use biometric authentication in MAUI
Find the NuGet package include prelease

Follow the guide on GitHub for how to set it up for your .NET MAUI project. The guide is currently located under the maui-support branch, so if the link doesn’t work it’s already been merged in and you can use the previously provided link.

Add a button with a click handler. Inside the event handler in the code-behind, add the following code:

var request = new AuthenticationRequestConfiguration("Prove you have fingers!", 
    "Because without it you can't have access");
var result = await CrossFingerprint.Current.AuthenticateAsync(request);
if (result.Authenticated)
{
	await DisplayAlert("Authenticated!", "Access granted", "Cool beans");
}
else
{
	await DisplayAlert("Not authenticated!", "Access denied", "aww");
}

Now when you click the button, you will be asked to authenticate yourself via facial recognition or fingerprint. Check the video below to see how it works on Android:

Abstractions

You can also do the same with dependency injection by using the IFingerprint interface and resolving it to CrossFingerprint.Current. Note that this may not be best practice when it comes to using DI (dependency injection).

using Plugin.Fingerprint.Abstractions;

namespace MauiBiometrics;

public partial class MainPage : ContentPage
{
    private readonly IFingerprint fingerprint;

    public MainPage(IFingerprint fingerprint)
    {
        InitializeComponent();
        this.fingerprint = fingerprint;
    }

    private async void OnCounterClicked(object sender, EventArgs e)
    {
        var request = new AuthenticationRequestConfiguration("Prove you have fingers!", 
            "Because without it you can't have access");
        var result = await fingerprint.AuthenticateAsync(request);
        if (result.Authenticated)
        {
            await DisplayAlert("Authenticated!", "Access granted", "Cool beans");
        }
        else
        {
            await DisplayAlert("Not authenticated!", "Access denied", "aww");
        }
    }
}

iOS Settings

As usual iOS and MacCatalyst are different. To have the biometric authentication working on them, you have to open the Info.plist under the folders iOS and MacCatalyst and add the following lines

<key>NSFaceIDUsageDescription</key>
<string>Need your face to unlock secrets!</string>

Android settings

For Android, the only setting we have to change is the Minimum Target Android Framework under the Application > Android Targets. Change the value to 31.0 as the fingerprint component requires.

Android Minimum Target Android Framework - Use biometric authentication in MAUI
Android Minimum Target Android Framework

Windows settings

For the Windows platform we don’t have to change nothing! When the application will require the biometric authentication, if you have a device that allows to use Windows Hello or another similar authentication, the Windows Security window will appear and check if it is you.

Windows biometric authentication - Use biometric authentication in MAUI
Windows biometric authentication

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.