Fingerprint Authentication using Xamarin.Forms

Why Fingerprint Authentication for mobile apps?

Fingerprint authentication technology for mobile devices has become the preferred user authentication solution in mobile device security. Adding fingerprint authentication to mobile applications improves the user experience

  • It is a quick and convenient way of authenticating user's identity
  • You can't forget a fingerprint
  • No more struggling with mobile keyboards
  • No more password resets or recovery
  • Impossible to guess since your fingerprint is unique
Let us see how to apply Fingerprint Authentication to Xamarin.Forms (PCL) project

I will be using Visual Studio 2015 for this tutorial, but similar approach is applicable for Xamarin Studio

Lets fire up Visual Studio and create a new Project as below:

Project creation screen

Select Blank App and Portable Class Library (PCL)

New Cross Platform App - FPAuthDemo

Alternative login using Fingerprint is not supported in UWP Project(s)

Include the Following Nuget Plugin for All projects
Install-Package Plugin.Fingerprint -Version 1.4.4  
Include the following Nuget Plugin for Android Project Only
Install-Package Plugin.CurrentActivity -Version 1.0.1  

Once the plugins are referenced, lets starting off with the auto created template code and modify the MainPage.xaml as below. This will include a button btnFPAuthentication

<?xml version="1.0" encoding="utf-8" ?>  
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:FPAuthDemo"
             x:Class="FPAuthDemo.MainPage">

    <StackLayout Orientation="Vertical">
        <Button x:Name="btnFPAuthentication" Text="Authenticate using Fingerprint"></Button>
    </StackLayout>

</ContentPage>  

On the code-behind - MainPage.xaml.cs , lets add the using statement for Plugin.FingerPrint and change the code as per below snippet:

public partial class MainPage : ContentPage  
{
    public MainPage()
    {
        InitializeComponent();

        btnFPAuthentication.Clicked += BtnFpAuthenticationOnClicked;
    }

    private async void BtnFpAuthenticationOnClicked(object sender, EventArgs eventArgs)
    {
        var isFingerPrintAuthenticationAvailable = await CrossFingerprint.Current.IsAvailableAsync(true);
        if (isFingerPrintAuthenticationAvailable)
        {
            var auth =
                await CrossFingerprint.Current.AuthenticateAsync("Authenticate yourself to access Awesome App");
            if (auth.Authenticated)
            {
                //Apply your logic for authenticated user
                await DisplayAlert("Authenication Successful", "You are Authenticated", "OK");
            }
            else
            {
                //Apply your fallback logic for invalid user
                await DisplayAlert("Authentication Failed", "You are not Authenticated", "OK");
            }

        }

    }
}

The function IsAvailableAsync tests if the Fingerprint is supported by the device and fallback authentication is available to use

var isFingerPrintAuthenticationAvailable = await CrossFingerprint.Current.IsAvailableAsync(true);  

And the below line of code determines if the fingerprint is a match with the stored fingerprints on device

CrossFingerprint.Current.AuthenticateAsync("Authenticate yourself to access Awesome App");  

Upto this point, we are good with Fingerprint authentication for iOS, but to include Fingerprint authentication on Android devices we will have to perform the few additional steps.

Open the MainActivity.cs file in Android project and add usings for Plugin.Fingerprint and Plugin.CurrentActivity plugins. Then after the base.OnCreate(...) add the following line of code.

CrossFingerprint.SetCurrentActivityResolver(() => CrossCurrentActivity.Current.Activity);  

Then go to Application manifest to check the following intent.

Required Permissions from Manifest

Now exeute the app on your device to test

Thats all you need to apply fingerprint authentication your Xamarin.Forms app

In the next post we will see how to

  • Test fingerprint authentication on Android Emulator
  • Test fingerprint authentication on iPhone Emulator (on Mac)

Happy coding...

Code for this post can be found here