Category Archives: Biometrics

Fingerprint and biometrics authentication in Xamarin Forms

This is something I’ve wanted to try for a while and there’s a NuGet package that will allow us to enable and use biometric authentication with very little effort – much of this post will end up covering the github README at Biometric / Fingerprint plugin for Xamarin. So I strongly recommend checking that out.

Create a sample application

Let’s create a new Xamarin Forms application to test this out. So follow these steps to get up and running…

  • In Visual Studio create a new project – Mobile App (Xamarin.Forms)
  • At the solution level, right mouse click in Visual Studio 2019 and select Manage NuGet Packages for Solution
  • Browse for Plugin.Fingerprint by Sven-Michael Stübe
  • Click on the package then check each of your projects, shared and platform specific. We need to add the plugin to all projects, then click Install
  • In the Android MainActivity.cs file, OnCreate method after Xamarin.Essentials.Platform.Init(this, savedInstanceState); add
    CrossFingerprint.SetCurrentActivityResolver(
       () => Xamarin.Essentials.Platform.CurrentActivity);
    
  • In the Android Manifest add Required Permissions USE_FINGERPRINT
  • In the iOS project, open the Info.plist in code (F7) and add the following
    <key>NSFaceIDUsageDescription</key>
    <string>Use your face to authenticate</string>
    

    Ofcourse the string can be whatever you want.

Now we’ve got the project and configuration set up you’ll want some popup, page or just a button on your MainPage.xaml to initiate the Fingerprint/Biometrics login. For now let’s just add a Button to the MainPage.xaml and, for brevity, just add a Clicked handler, so for example

<Button Clicked="Button_OnClicked" Text="Authenticate with Biometrics" />

and here’s the code within the code behind for Button_OnClicked

private async void Button_OnClicked(object sender, EventArgs e)
{
   if (await CrossFingerprint.Current.IsAvailableAsync(true))
   {
      var result = await CrossFingerprint.Current.AuthenticateAsync(
         new AuthenticationRequestConfiguration("Login", "Access your account"));
      if (result.Authenticated)
      {
         await DisplayAlert("Success", "Authenticated", "OK");
      }
      else
      {
         await DisplayAlert("Failure", "Not Authenticated", "OK");
      }
   }
   else
   {
      await DisplayAlert("Failure", "Biometrics not available", "OK");
   }
}

We begin by checking if biometrics are available, passing in true will allow fallback to pin authentication. Assuming biometrics are available we then display the authentication mechanism using AuthenticateAsync passing in configuration that, in this case, will display some text on the fingerprint popup. If we’re authenticate then we display an alert to show success, in this example, but ofcourse you’ll handle success and failure as needed by your application.

Testing in the Android emulator

To test this application in the Android emulator

  • Goto the Settings within the Android OS and select Security
  • Under Device Security select Screen lock and add a pin
  • Under Device Security select Fingerprint and add a fingerprint, now to actually add a fingerprint we’ll click the … on the emulator and select Fingerprint, then click the Touch the Sensor button twice – you’re see the Fingerprint dialog go 50% of the way then 100% on the second click, finally click Done

Once we’re set up the security on the emulator and supplied one or more fingerprints run up your Xamarin Forms application and click the button you added. You’ll be presented with the AuthenticationRequestConfiguration you added, again using the … button on the emulator (if you closed the Extended controls dialog), select Fingerprint and click Touch the Sensore – this basically emulates a fingerprint touching the sensor.

To test for success, ensure the Fingerprint selected is one you added, i.e. by default Finger 1, to test for failure simply select one of the other, non-configured fingers and click Touch the Sensor

Testing in the iOS simulator

The setup for testing using the iOS simulator is a little simpler than Android…

  • Open the simulator and (in the latest XCode I’m using 13 but basically 12.x and above) select Features | Touch ID or Face ID (whichever is available on your simulator) and check the Enrolled option to show a tick (untick to remove the feature).

Now when you click your authentication button in your Xamarin forms application you may be presented with the dialog to allow the permission to be used, once you’ve accepted this you won’t see it again. Next you’ll see a small grey square which will denote your Face ID authentication (or for Touch ID you’ll get the fingerprint dialog), from the simulator’s Features and Face ID submenu, select Matching Face to simulate a successful authentication or Non-matching Face for a failure. For Touch ID simulators select Matching Touch for successful authentication or Non-matching Touch for a failure.

Code

Code for this post is available on GitHub.

References

Biometric / Fingerprint plugin for Xamarin
Enrolling a Fingerprint