Implementing Biometric Authentication In .NET MAUI

In this article, I will show how easy it is to add biometric authentication in .NET MAUI.

biometric authentication in .NET MAUI

Without a doubt, biometric authentication has increasingly become an integral part of mobile applications. It ensures that the user is the rightful owner of the device they are using. There are two ways to authenticate: one is through Face ID (iOS) and the other through fingerprint (Android / iOS). In this case, we'll use our .NET MAUI application.

Today we will see how to integrate biometric authentication in a .NET MAUI project. So, let's get started.

How to use

The first thing to do is install the plugin through the package manager in the .NET MAUI project.

In the MAUI .NET project, install the Plugin.Fingerprint NuGet package. You will need version 3.0.0-beta.1, which is currently in preview, so remember to check “Include Preview”:

biometric authentication in .NET MAUI

Implementation - Android 

Now, set the target SDK version. The target SDK version must be >= 6.0. For my part, and if possible, I recommend always using the latest stable version of the SDK. You can set the target SDK version in the Android project properties.

Install Android X Migration 

Since version 2, the aforementioned plugin uses Android X. Therefore, Xamarin.AndroidX.Migration must be installed in our Android project.

Request permission in AndroidManifest.xml 

Next, we must add the permission in the AndroidManifest.xml file.

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

biometric authentication in .NET MAUI

To finish with Android, in the MainActivity.cs file, let's add the corresponding using:

using Plugin.Fingerprint;

Later, in the same file, add the OnCreate() method. Remember, in this method, the basic startup logic of the application is executed. This must occur only once in the entire life of the activity. Here we will add the following line of code:

CrossFingerprint.SetCurrentActivityResolver(() => this);

Our file will look like this:

biometric authentication in .NET MAUI

Implementation – iOS 

In iOS authentication is simpler. You just have to add the “NSFaceIDUsageDescription” to the Info.plist file to describe the reason why our application will use Face ID (see Documentation). Otherwise, the app will crash when you initiate a Face ID authentication on iOS 11.3+.

<key>NSFaceIDUsageDescription</key>
<string>Need your face to unlock magic things</string>

Note: If you have any questions about how to implement this, I recommend that you follow the guide on GitHub to learn more about how to configure it for your .NET MAUI project. It's worth mentioning that the guide is currently in the Maui support branch, so if the link doesn't work, it's already been merged and you can use the link provided above.

MauiProgram.cs

Finally, and before going to modify our XAML and add the main functionality, in the MauiProgram.cs file we must add the following lines:

builder.Services.AddSingleton<MainPage>();
builder.Services.AddSingleton(typeof(IFingerprint), CrossFingerprint.Current);

Staying as follows:

biometric authentication in .NET MAUI

XAML

Next, add a button with a click handler (or modify the one the solution created for us if the project is new):

<Button 
      Text="Authenticate"
      FontAttributes="Bold"
      SemanticProperties.Hint="Click to authenticate with your fingerprint"
      Clicked="OnBiometricClicked"
      HorizontalOptions="Center" />

Next, we must add the corresponding using:

using Plugin.Fingerprint.Abstractions;

Then declare the IFingerprint interface:

private readonly IFingerprint fingerprint;

The same as passed as parameter:

public MainPage(IFingerprint fingerprint)

After that, we will declare inside the MainPage:

InitializeComponent();
this.fingerprint = fingerprint;

Now, inside the "OnBiometricClicked" event handler, in the code-behind add the following code. When you click the button, it will ask you to authenticate via fingerprint:

private async void OnBiometricClicked(object sender, EventArgs e)
{
    var request = new AuthenticationRequestConfiguration("Validate that you have fingers", "Because without them you will not be able to access");
    var result = await fingerprint.AuthenticateAsync(request);
    if (result.Authenticated)
    {
        await DisplayAlert("Authenticate!", "Access Granted", "OK");
    }
    else
    {
        await DisplayAlert("Unauthenticated", "Access Denied", "OK");
    }
}

And voila, that's it! 

Result / Output - Android

biometric authentication in .NET MAUI

As always, I've provided a code example on my GitHub below, which you can check out if you'd like.

Summary

I hope this little article has given you enough information to apply such control in your .NET MAUI applications and see the expected results.

I invite you to leave a comment if you want me to give more details about anything in this article.

More Information: GitHub Xamarin-Fingerprint

Happy coding!