Finger Print Authentication Using Xamarin.Forms

Introduction

With technology infiltrating every aspect of modern living and our world becoming increasingly digitized, protecting confidential information becomes all the more difficult. Passwords and keys were once considered sufficient to provide data security but now, they look increasingly feeble in the face of sophisticated hacker attacks. In fact, passwords are the weakest link in an organization’s security system. This is because they are shareable and even those with strong entropy can be cracked by a variety of methods. The recent reports of network security breaches and identity thefts further affirm the fact that a strong authentication method is needed .

Prerequisites
  1. Basic programming knowledge of C#.
  2. Basic knowledge of Xamarin.
  3. A physical device which contains a Finger Print sensor.
Implementation

After creating a cross-platform project, we should install the NuGet package in the solution

Plugin.Fingerprint
(Right-click on the solution / Manage NuGet Packages for Solution )



After that, we will create our MainPage which contains a Switch and a Button: If the switch is enabled, it will close the pop-up authentication after 10 seconds, and the button will show the pop-up authentication.
  1. <StackLayout Orientation="Vertical" Padding="10">  
  2.     <Label x:Name="lblAuthenticationType"></Label>  
  3.     <StackLayout Orientation="Horizontal">  
  4.         <StackLayout.IsVisible>  
  5.             <OnPlatform x:TypeArguments="x:Boolean" iOS="True" Android="True" WinPhone="False" /> </StackLayout.IsVisible>  
  6.         <Switch x:Name="swAutoCancel"></Switch>  
  7.         <Label Text="Cancel after 10sec"></Label> </StackLayout>  
  8.     <Button Text="Authenticate" Clicked="OnAuthenticate"></Button> </StackLayout>  
Now, we should write our code behind: first we will create an asynchronous method called "AuthenticationAsync" and an attribute called _cancel 
  1. private CancellationTokenSource _cancel;  
  2. private async Task AuthenticationAsync(string reason, string cancel = null, string fallback = null, string tooFast = null) {  
  3.     _cancel = swAutoCancel.IsToggled ? new CancellationTokenSource(TimeSpan.FromSeconds(10)) : new CancellationTokenSource();  
  4.     var dialogConfig = new AuthenticationRequestConfiguration(reason) {  
  5.         CancelTitle = cancel,  
  6.             FallbackTitle = fallback,  
  7.             UseDialog = true  
  8.     };  
  9.     var result = await Plugin.Fingerprint.CrossFingerprint.Current.AuthenticateAsync(dialogConfig, _cancel.Token);  
  10.     await SetResultAsync(result);  
  11. }  
So, after that we need to create the SetResultAsync method which will contain the result of our FingerPrint authentication: 
  1. private async Task SetResultAsync(FingerprintAuthenticationResult result) {  
  2.     if (result.Authenticated) {  
  3.         await DisplayAlert("FingerPrint Sample""Success""Ok");  
  4.     } else {  
  5.         await DisplayAlert("FingerPrint Sample", result.ErrorMessage, "Ok");  
  6.     }  
  7. }  
Now, to run our application on Android we need to add some specific settings on our Android project: 
First of all, we need to allow the permission to use the FingerPrint by right-clicking on the Android project ==> select Properties==> Android Manifest tab and on the bottom of this page we select "USE_FINGERPRINT" 

 

Now, we should add two classes "MyCustomDialogFragment" and "MainApplication" : 
  1. [Application]  
  2. public class MainApplication: Application, Application.IActivityLifecycleCallbacks {  
  3.     public MainApplication(IntPtr handle, JniHandleOwnership transer): base(handle, transer) {}  
  4.     public override void OnCreate() {  
  5.         base.OnCreate();  
  6.         RegisterActivityLifecycleCallbacks(this);  
  7.         CrossFingerprint.SetCurrentActivityResolver(() => CrossCurrentActivity.Current.Activity);  
  8.     }  
  9.     public override void OnTerminate() {  
  10.         base.OnTerminate();  
  11.         UnregisterActivityLifecycleCallbacks(this);  
  12.     }  
  13.     public void OnActivityCreated(Activity activity, Bundle savedInstanceState) {  
  14.         CrossCurrentActivity.Current.Activity = activity;  
  15.     }  
  16.     public void OnActivityDestroyed(Activity activity) {}  
  17.     public void OnActivityPaused(Activity activity) {}  
  18.     public void OnActivityResumed(Activity activity) {  
  19.         CrossCurrentActivity.Current.Activity = activity;  
  20.     }  
  21.     public void OnActivitySaveInstanceState(Activity activity, Bundle outState) {}  
  22.     public void OnActivityStarted(Activity activity) {  
  23.         CrossCurrentActivity.Current.Activity = activity;  
  24.     }  
  25.     public void OnActivityStopped(Activity activity) {}  
  26. }  
  27. public class MyCustomDialogFragment: FingerprintDialogFragment {  
  28.     public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  29.         var view = base.OnCreateView(inflater, container, savedInstanceState);  
  30.         view.Background = new ColorDrawable(Color.Magenta);  
  31.         return view;  
  32.     }  
  33. }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Plugin.Fingerprint.Dialog;
namespace FingerPrintSample.Droid
{
   public class MyCustomDialogFragment : FingerprintDialogFragment
   {
      public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
      {
         var view = base.OnCreateView(inflater, container, savedInstanceState);
         view.Background = new ColorDrawable(Color.Magenta);
         return view;
      }
   }
}
 
This is a path to the GitHub repository: https://github.com/slim8791/FingerPrintSample-Xamarin-Forms.