{"id":8928,"date":"2021-10-05T21:55:48","date_gmt":"2021-10-05T21:55:48","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8928"},"modified":"2021-10-05T22:07:53","modified_gmt":"2021-10-05T22:07:53","slug":"fingerprint-and-biometrics-authentication-in-xamarin-forms","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/fingerprint-and-biometrics-authentication-in-xamarin-forms\/","title":{"rendered":"Fingerprint and biometrics authentication in Xamarin Forms"},"content":{"rendered":"<p>This is something I&#8217;ve wanted to try for a while and there&#8217;s a NuGet package that will allow us to enable and use biometric authentication with very little effort &#8211; much of this post will end up covering the github README at <a href=\"https:\/\/github.com\/smstuebe\/xamarin-fingerprint\" rel=\"noopener\" target=\"_blank\">Biometric \/ Fingerprint plugin for Xamarin<\/a>. So I strongly recommend checking that out.<\/p>\n<p><strong>Create a sample application<\/strong><\/p>\n<p>Let&#8217;s create a new Xamarin Forms application to test this out. So follow these steps to get up and running&#8230;<\/p>\n<ul>\n<li>In Visual Studio create a new project &#8211; Mobile App (Xamarin.Forms)<\/li>\n<li>At the solution level, right mouse click in Visual Studio 2019 and select <em>Manage NuGet Packages for Solution<\/em><\/li>\n<li>Browse for Plugin.Fingerprint by Sven-Michael St\u00fcbe<\/li>\n<li>Click on the package then check each of your projects, shared and platform specific. We need to add the plugin to all projects, then click Install<\/li>\n<li>In the Android MainActivity.cs file, OnCreate method after <em>Xamarin.Essentials.Platform.Init(this, savedInstanceState);<\/em> add\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nCrossFingerprint.SetCurrentActivityResolver(\r\n   () =&gt; Xamarin.Essentials.Platform.CurrentActivity);\r\n<\/pre>\n<\/li>\n<li>In the Android Manifest add Required Permissions <strong>USE_FINGERPRINT<\/strong><\/li>\n<li>In the iOS project, open the Info.plist in code (F7) and add the following\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;key&gt;NSFaceIDUsageDescription&lt;\/key&gt;\r\n&lt;string&gt;Use your face to authenticate&lt;\/string&gt;\r\n<\/pre>\n<p>Ofcourse the string can be whatever you want.\n<\/li>\n<\/ul>\n<p>Now we&#8217;ve got the project and configuration set up you&#8217;ll want some popup, page or just a button on your MainPage.xaml to initiate the Fingerprint\/Biometrics login. For now let&#8217;s just add a Button to the MainPage.xaml and, for brevity, just add a Clicked handler, so for example<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;Button Clicked=&quot;Button_OnClicked&quot; Text=&quot;Authenticate with Biometrics&quot; \/&gt;\r\n<\/pre>\n<p>and here&#8217;s the code within the code behind for Button_OnClicked<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprivate async void Button_OnClicked(object sender, EventArgs e)\r\n{\r\n   if (await CrossFingerprint.Current.IsAvailableAsync(true))\r\n   {\r\n      var result = await CrossFingerprint.Current.AuthenticateAsync(\r\n         new AuthenticationRequestConfiguration(&quot;Login&quot;, &quot;Access your account&quot;));\r\n      if (result.Authenticated)\r\n      {\r\n         await DisplayAlert(&quot;Success&quot;, &quot;Authenticated&quot;, &quot;OK&quot;);\r\n      }\r\n      else\r\n      {\r\n         await DisplayAlert(&quot;Failure&quot;, &quot;Not Authenticated&quot;, &quot;OK&quot;);\r\n      }\r\n   }\r\n   else\r\n   {\r\n      await DisplayAlert(&quot;Failure&quot;, &quot;Biometrics not available&quot;, &quot;OK&quot;);\r\n   }\r\n}\r\n<\/pre>\n<p>We begin by checking if biometrics are available, passing in true will allow fallback to pin authentication. Assuming biometrics are available we then display the authentication mechanism using <em>AuthenticateAsync<\/em> passing in configuration that, in this case, will display some text on the fingerprint popup. If we&#8217;re authenticate then we display an alert to show success, in this example, but ofcourse you&#8217;ll handle success and failure as needed by your application.<\/p>\n<p><strong>Testing in the Android emulator<\/strong><\/p>\n<p>To test this application in the Android emulator<\/p>\n<ul>\n<li>Goto the Settings within the Android OS and select Security<\/li>\n<li>Under Device Security select Screen lock and add a pin<\/li>\n<li>Under Device Security select Fingerprint and add a fingerprint, now to actually add a fingerprint we&#8217;ll click the &#8230; on the emulator and select Fingerprint, then click the <em>Touch the Sensor<\/em> button twice &#8211; you&#8217;re see the Fingerprint dialog go 50% of the way then 100% on the second click, finally click Done<\/li>\n<\/ul>\n<p>Once we&#8217;re set up the security on the emulator and supplied one or more fingerprints run up your Xamarin Forms application and click the button you added. You&#8217;ll be presented with the <em>AuthenticationRequestConfiguration<\/em> you added, again using the &#8230; button on the emulator (if you closed the Extended controls dialog), select Fingerprint and click <em>Touch the Sensore<\/em> &#8211; this basically emulates a fingerprint touching the sensor.<\/p>\n<p>To test for success, ensure the Fingerprint selected is one you added, i.e. by default Finger 1, to test for failure simply select one of the other, non-configured fingers and click <em>Touch the Sensor<\/em><\/p>\n<p><strong>Testing in the iOS simulator<\/strong><\/p>\n<p>The setup for testing using the iOS simulator is a little simpler than Android&#8230;<\/p>\n<ul>\n<li>Open the simulator and (in the latest XCode I&#8217;m using 13 but basically 12.x and above) select Features | Touch ID or Face ID (whichever is available on your simulator) and check the Enrolled option to show a tick (untick to remove the feature).<\/li>\n<\/ul>\n<p>Now when you click your authentication button in your Xamarin forms application you may be presented with the dialog to allow the permission to be used, once you&#8217;ve accepted this you won&#8217;t see it again. Next you&#8217;ll see a small grey square which will denote your Face ID authentication (or for Touch ID you&#8217;ll get the fingerprint dialog), from the simulator&#8217;s Features and Face ID submenu, select <em>Matching Face<\/em> to simulate a successful authentication or <em>Non-matching Face<\/em> for a failure. For Touch ID simulators select <em>Matching Touch<\/em> for successful authentication or <em>Non-matching Touch<\/em> for a failure.<\/p>\n<p><strong>Code<\/strong><\/p>\n<p>Code for this post is available on <a href=\"https:\/\/github.com\/putridparrot\/blog-projects\/tree\/master\/BiometricTest\" rel=\"noopener\" target=\"_blank\">GitHub<\/a>.<\/p>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/github.com\/smstuebe\/xamarin-fingerprint\" rel=\"noopener\" target=\"_blank\">Biometric \/ Fingerprint plugin for Xamarin<\/a><br \/>\n<a href=\"https:\/\/docs.microsoft.com\/en-us\/xamarin\/android\/platform\/fingerprint-authentication\/enrolling-fingerprint\" rel=\"noopener\" target=\"_blank\">Enrolling a Fingerprint<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is something I&#8217;ve wanted to try for a while and there&#8217;s a NuGet package that will allow us to enable and use biometric authentication with very little effort &#8211; much of this post will end up covering the github README at Biometric \/ Fingerprint plugin for Xamarin. So I strongly recommend checking that out. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[318,122],"tags":[],"class_list":["post-8928","post","type-post","status-publish","format-standard","hentry","category-biometrics","category-xamarin-forms"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8928"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8928\/revisions"}],"predecessor-version":[{"id":8936,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8928\/revisions\/8936"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}