Click here to Skip to main content
13,737,212 members
Click here to Skip to main content
Add your own
alternative version

Stats

5.7K views
7 bookmarked
Posted 13 Mar 2018
Licenced CPOL

Finger Print Authentication using Xamarin Forms

, 13 Mar 2018
Rate this:
Please Sign up or sign in to vote.
This is a tutorial which contains the 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 the need of today’s time.

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, 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.

<StackLayout Orientation="Vertical" Padding="10">
   <Label x:Name="lblAuthenticationType"></Label>
   <StackLayout Orientation="Horizontal">
      <StackLayout.IsVisible>
         <OnPlatform x:TypeArguments="x:Boolean"
                      iOS="True"
                      Android="True"
                      WinPhone="False" />
       </StackLayout.IsVisible>
    <Switch x:Name="swAutoCancel"></Switch>
    <Label Text="Cancel after 10sec"></Label>
   </StackLayout>
   <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.

private CancellationTokenSource _cancel;

private async Task AuthenticationAsync
(string reason, string cancel = null, string fallback = null, string tooFast = null)
{
   _cancel = swAutoCancel.IsToggled ? new CancellationTokenSource
             (TimeSpan.FromSeconds(10)) : new CancellationTokenSource();

   var dialogConfig = new AuthenticationRequestConfiguration(reason)
   {
     CancelTitle = cancel,
     FallbackTitle = fallback,
     UseDialog = true
   };

   var result = await Plugin.Fingerprint.CrossFingerprint.Current.AuthenticateAsync
                (dialogConfig, _cancel.Token);

   await SetResultAsync(result);
}

So, after that, we need to create the SetResultAsync method which will contain the result of our FingerPrint authentication:

private async Task SetResultAsync(FingerprintAuthenticationResult result)
{
   if (result.Authenticated)
   {
       await DisplayAlert("FingerPrint Sample", "Success", "Ok");
   }
   else
   {
      await DisplayAlert("FingerPrint Sample", result.ErrorMessage, "Ok");
   }
}

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":

[Application]
public class MainApplication : Application, Application.IActivityLifecycleCallbacks
{
   public MainApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer)
   {
   }
   public override void OnCreate()
   {
      base.OnCreate();
      RegisterActivityLifecycleCallbacks(this);
      CrossFingerprint.SetCurrentActivityResolver(() => CrossCurrentActivity.Current.Activity);
   }
   public override void OnTerminate()
   {
      base.OnTerminate();
      UnregisterActivityLifecycleCallbacks(this);
   }
   public void OnActivityCreated(Activity activity, Bundle savedInstanceState)
   {
      CrossCurrentActivity.Current.Activity = activity;
   }
   public void OnActivityDestroyed(Activity activity)
   {
   }
   public void OnActivityPaused(Activity activity)
   {
   }
   public void OnActivityResumed(Activity activity)
   {
      CrossCurrentActivity.Current.Activity = activity;
   }
   public void OnActivitySaveInstanceState(Activity activity, Bundle outState)
   {
   }
   public void OnActivityStarted(Activity activity)
   {
      CrossCurrentActivity.Current.Activity = activity;
   }
   public void OnActivityStopped(Activity activity)
   {
   }
}
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;
   }
}

So this is a path of the GitHub repository: https://github.com/slim8791/FingerPrintSample-Xamarin-Forms.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Slim Hammami
Tunisia Tunisia
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 13 Mar 2018
Article Copyright 2018 by Slim Hammami
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid