Xamarin.Forms - Fingerprint Authentication In your App

Introduction

 
Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
 
Plugin.Fingerprint
 
Authenticate a user via fingerprint, face id or any other biometric / local authentication method from a cross platform API.
 
Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project

 
Start by creating a new Xamarin.Forms project. You wíll learn more by going through the steps yourself.
 
Create a new or existing Xamarin forms(.Net standard) Project. With Android and iOS Platform.
 
 
Nuget
 
Install the following Nuget from Nuget Manager In your Visual Studio.
  • Plugin.Fingerprint
 
iOS Setup
 
Add NSFaceIDUsageDescription to your Info.plist to describe the reason your app uses Face ID. Otherwise the App will crash when you start a Face ID authentication on iOS 11.3+.
 
<key>NSFaceIDUsageDescription</key> 
<string>Need your face to unlock secrets!</string> 
 
Android Setup
 
Request the permission in AndroidManifest.xml
 
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
 
Add the below code in MainActivity.cs OnCreate Method.
  1. CrossFingerprint.SetCurrentActivityResolver(() => this);  
Install Android X Migration
 
Since version 2, this plugin uses Android X. You have to install Xamarin.AndroidX.Migration in your Android project.
 
FingerPrint Authentication
  1. private async Task AuthenticateAsync(string reason, string cancel = nullstring fallback = nullstring tooFast = null)  
  2.         {  
  3.             _cancel = false ? new CancellationTokenSource(TimeSpan.FromSeconds(10)) : new CancellationTokenSource();  
  4.               
  5.             var dialogConfig = new AuthenticationRequestConfiguration("My App", reason)  
  6.             {   
  7.                 CancelTitle = cancel,  
  8.                 FallbackTitle = fallback,  
  9.                 AllowAlternativeAuthentication = true,  
  10.                 ConfirmationRequired = false  
  11.             };  
  12.   
  13.             dialogConfig.HelpTexts.MovedTooFast = tooFast;  
  14.             var result = await Plugin.Fingerprint.CrossFingerprint.Current.AuthenticateAsync(dialogConfig, _cancel.Token);  
  15.             await SetResultAsync(result);  
  16.         }  
Enable AlternativeAuthentication
 
If you need to allow alternate authentication such as Pin, Pattern and password set AlternativeAuthentication=true
  1. var dialogConfig = new AuthenticationRequestConfiguration("My App", reason)  
  2.             {   
  3.                 CancelTitle = cancel,  
  4.                 FallbackTitle = fallback,  
  5.                 AllowAlternativeAuthentication = true,  
  6.                 ConfirmationRequired = false  
  7.             };   
Full code
  1. protected override async void OnAppearing()  
  2.         {  
  3.             base.OnAppearing();  
  4.   
  5.             var authType= await Plugin.Fingerprint.CrossFingerprint.Current.GetAuthenticationTypeAsync();  
  6.             if(authType==AuthenticationType.Fingerprint)  
  7.             {  
  8.                 lblAuthenticationType.Text = "Auth Type: " + authType;  
  9.   
  10.                 await AuthenticateAsync("Authendicate with Touch ID");  
  11.             }  
  12.              
  13.         }  
  14.   
  15.         private async Task AuthenticateAsync(string reason, string cancel = nullstring fallback = nullstring tooFast = null)  
  16.         {  
  17.             _cancel = false ? new CancellationTokenSource(TimeSpan.FromSeconds(10)) : new CancellationTokenSource();  
  18.               
  19.             var dialogConfig = new AuthenticationRequestConfiguration("My App", reason)  
  20.             {   
  21.                 CancelTitle = cancel,  
  22.                 FallbackTitle = fallback,  
  23.                 AllowAlternativeAuthentication = true,  
  24.                 ConfirmationRequired = false  
  25.             };  
  26.   
  27.             dialogConfig.HelpTexts.MovedTooFast = tooFast;  
  28.             var result = await Plugin.Fingerprint.CrossFingerprint.Current.AuthenticateAsync(dialogConfig, _cancel.Token);  
  29.             await SetResultAsync(result);  
  30.         }  
Run
 
 
 
 
 
I hope you have understood and learned how to implement fingerprint, password and facelock authentication in your XamarinApp.
 
Thanks for reading. Please share your comments and feedback. Happy Coding :)


Similar Articles