Adding fingerprint authentication in Xamarin Forms
Tags: xamarin, authentication, fingerprint, login
Published: 2019-10-13 19:15:43

---
title: Adding fingerprint authentication in Xamarin Forms
published: true
date: 2019-04-11 06:25:42 UTC
tags: Xamarin,authentication,fingerprint,login
canonical_url: https://blog.torib.io/2019/04/11/adding-fingerprint-authentication-on-xamarin-forms/
---

Adding a fingerprint authentication for multiplatform projects have never been easier than with the [Plugin](https://github.com/smstuebe/xamarin-fingerprint)<g id="184" data-gr-id="184"><a href="https://github.com/smstuebe/xamarin-fingerprint">.Fingerprint</a></g> <g id="229" data-gr-id="229">nuget</g> package.

## But first…

A few things to clarify about biometric/fingerprint authentication. These are implementation limitations by the technology itself and not introduced in any way by the Xamarin Forms implementation:

1. You cannot save “the fingerprint”. There are two reasons for this:
  - The detection of the fingerprint is done [inside the hardware](https://www.androidcentral.com/how-does-android-save-your-fingerprints) that reads the finger. 
  - By law you **cannot** , **should not** and for now, **won’t** have access to this information
2. If your device has more than one registered fingerprint you **cannot** tell which fingerprint was used to authenticate.
3. You can only tell if the sensor has or hasn’t received a valid fingerprint.

## Now to the main event

To start you want to include the Plugin.Fingerprint into all your solution projects.

![](https://blog.torib.io/wp-content/uploads/2019/04/Screen-Shot-2019-04-11-at-1.19.21-AM.png)

Then you have to do a little bit of setup. Don’t be scared it’s just three lines for Android and a couple lines for iOS.

### On Android:

- Request the permissions needed inside your AndroidManifest.xml
- Set the Activity running the app so the plugin can show the request dialog when asking for the fingerprint. In the plugin repository, they use the CurrentActivity plugin but I noticed that you can just set it up inside the MainActivity by adding the **this** keyword
<script src="https://gist.github.com/eatskolnikov/09645594e562dc643e761acc914ff34b.js"></script>
### On iOS:

- Because this plugin works for both Face and Fingerprint authentication, according to the [iOS documentation,](https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW75) you need to add an entry for **NSFaceIDUsageDescription** into the **Info.** <g id="6" data-gr-id="6"><g id="6" data-gr-id="6"><strong>plist</strong></g></g> file. If you do not add this entry the plugin simply won’t work and your app may even crash. This entry describes what you will be using the face id for in case you were to use the face authentication.
<script src="https://gist.github.com/eatskolnikov/8790d9980ff8d03a5acf052f2d20b5d9.js"></script>

After the setup is done you need to call the AuthenticateAsync method. You should pass the text you want the user to see when you ask them to authenticate. The return is a boolean value that indicates whether the fingerprint was of a valid user or the user canceled the dialog. It also returns a false right away if there are no registered <g id="94" data-gr-id="94">fingerpr</g>ints.

<script src="https://gist.github.com/eatskolnikov/ab7a7546b65123d190599e2cbae2c2aa.js"></script>

![](https://blog.torib.io/wp-content/uploads/2019/04/fingerprint.gif)<figcaption>Running the example on a OnePlus6</figcaption>

## Bonus content: Configuring your emulators and simulators for fingerprint authentication

### On the iOS simulator:

- Make sure you “enroll” the Touch ID.
- To send a valid or invalid Touch ID you can use the options in the menu or the following keyboard shortcuts Shft+Cmd+M and Shift+Cmd+N

![](https://blog.torib.io/wp-content/uploads/2019/04/Screen-Shot-2019-04-11-at-2.01.03-AM.png)<figcaption>Enrolling the Touch ID on an iPhone 8 plus simulator<br></figcaption>

### On the Android emulator:

- Just go to the emulator options and see the Fingerprints section. From there you get access to a fixed number of the fingerprint.
- Proceed to register them into the device.

![](https://blog.torib.io/wp-content/uploads/2019/04/Screen-Shot-2019-04-11-at-2.16.39-AM.png)<figcaption>Looking for the fingerprint management section</figcaption>

![](https://blog.torib.io/wp-content/uploads/2019/04/Screen-Shot-2019-04-11-at-2.17.18-AM.png)<figcaption>Configuring the fingerprint. Just tap the “Touch this sensor” button from the emulator options whenever you get asked</figcaption>

I hope you find this useful. I think something like this could be very useful to include in Xamarin.Essentials. [From what I’ve gathered](https://github.com/xamarin/Essentials/issues/71) they are in plans of adding it but haven’t yet so maybe this is a good chance to do something about it… Anyways, have a good day and if you liked this article I encourage you to share it on social media or fax it to your dad and your mom, they will probably love it, trust me.

The post [Adding fingerprint authentication in Xamarin Forms](https://blog.torib.io/2019/04/11/adding-fingerprint-authentication-on-xamarin-forms/) appeared first on [Enmanuel Toribio](https://blog.torib.io).