Skip to content

Learn Mobile Development

Alamgeer's Blog

Menu
  • Home
  • Privacy Policy
Menu
Biometric authentication in Xamarin

Implementation Of Biometric Authentication in Xamarin Forms

Posted on November 28, 2022January 18, 2023 by Learn Mobile Development

Now a days most of the mobile applications that use biometric authentication to secure data access. This allows users to authenticate via the fingerprint sensor and the face ID on those devices that have these capabilities. In this article, I’m going to show you how to add Biometric Authentication in Xamarin Forms application.

Let’s Start

In this sample demo we will use Plugin.Fingerprint nuget package.

Plugin.Fingerprint

Plugin.Fingerprint enables us to authenticate the user via fingerprint or face id or any other biometric.

1 – Install Plugin.Fingerprint nuget

Xamarin Forms Biometric Authentication

2 – iOS Setup

2.1 – Add “NSFaceIDUsageDescription” to your Info.plist to describe the reason your app uses Face ID

<key>NSFaceIDUsageDescription</key>
<string>Need your face to unlock secrets!</string>

thats it for iOS Let’s move to android setup.

3 – Android Setup

3.1 – Add “USE_FINGERPRINT” permission in manifest file

<uses-permission android:name="android.permission.USE_FINGERPRINT" />

Set “Resolver” in “OnCreate()” method of MainActivity.cs

CrossFingerprint.SetCurrentActivityResolver(() => Xamarin.Essentials.Platform.CurrentActivity);

Now, Setup is completed for android as well.

4 – Setting up the UI

4.1 – Creating a MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="XFBiometric.MainPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

    <StackLayout>
        <Frame
            Padding="24"
            BackgroundColor="#2196F3"
            CornerRadius="0">
            <Label
                Margin="0,20,0,0"
                FontSize="36"
                HorizontalTextAlignment="Center"
                Text="Biometric Sample"
                TextColor="White" />
        </Frame>


        <StackLayout
            x:Name="StackContainer"
            Margin="0,150,0,0"
            IsVisible="False"
            VerticalOptions="CenterAndExpand">

            <Image
                x:Name="ImgSuccess"
                HeightRequest="200"
                WidthRequest="200" />

            <Label
                x:Name="LblMsg"
                FontAttributes="Bold"
                FontSize="25"
                HorizontalTextAlignment="Center"
                TextColor="Gray" />

        </StackLayout>

        <Button
            x:Name="BtnAuthenticate"
            Margin="0,0,0,50"
            Padding="20,15"
            BackgroundColor="#2196F3"
            Clicked="OnAuthenticateClicked"
            CornerRadius="30"
            FontAttributes="Bold"
            FontSize="20"
            HorizontalOptions="Center"
            Text="Authenticate"
            TextColor="White"
            VerticalOptions="EndAndExpand" />

    </StackLayout>
</ContentPage>

4.2 – MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Plugin.Fingerprint;
using Plugin.Fingerprint.Abstractions;
using Xamarin.Forms;

namespace XFBiometric
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        async void OnAuthenticateClicked(System.Object sender, System.EventArgs e)
        {
            var availability = await CrossFingerprint.Current.IsAvailableAsync();

            if (!availability)
            {
                StackContainer.IsVisible = false;
                await DisplayAlert("Warning!", "No biometrics available", "OK");

                return;
            }

            var authResult = await CrossFingerprint.Current.AuthenticateAsync(new AuthenticationRequestConfiguration("Heads up!", "Please validate biometric !!"));

            if (authResult.Authenticated)
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    StackContainer.IsVisible = true;
                    ImgSuccess.Source = ImageSource.FromFile("success.png");
                    LblMsg.Text = "Authentication Successfully !!";
                });
            }
            else
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    StackContainer.IsVisible = true;
                    ImgSuccess.Source = ImageSource.FromFile("cross.jpg");
                    LblMsg.Text = "Authentication Failed !!";
                });
            }
        }
    }
}

5 – Result

That’s all for now!

You can check the full source code here.

Happy Coding! 😀

You may also like

Item reordering to CollectionView

How to apply item reordering to CollectionView GridItemsLayout in MAUI

MediaElement in Xamarin Forms

How to use MediaElement in Xamarin Forms

Post Views: 277
Share article with

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

I'm Alamgeer Ashraf and having 9+ years of experience in enterprises mobile application development with Xamarin Native, Xamarin Forms & .Net MAUI.

Archives

  • February 2023 (1)
  • January 2023 (9)
  • December 2022 (5)
  • November 2022 (6)

Latest Posts

  • Prevent Dark Mode in Xamarin forms
    How to Prevent Dark Mode in Xamarin formsFebruary 3, 2023
  • apply color to Images
    How to apply color to Images and Icons in MAUIJanuary 30, 2023
  • iOS Large Page Title
    How to set iOS large page title in MAUIJanuary 26, 2023
  • Change StatusBar Color
    How to change StatusBar color in MAUIJanuary 22, 2023
  • Item reordering to CollectionView
    How to apply item reordering to CollectionView GridItemsLayout in MAUIJanuary 18, 2023

Popular Posts

  • How to use ObservableObject in MAUI for Data Binding
  • How to use drag and drop gesture to CollectionView in MAUI
  • How to Prevent Dark Mode in Xamarin forms

Latest Comments

  1. Randyperly on How to use StateContainer in MAUIOctober 18, 2024

    very good _________________ netgame casino бездепозитный бонус

  2. SmiSew on How to use StateContainer in MAUIOctober 10, 2024

    Thanks for the post _________________ pin up casino бест

  3. StavkiTarve on How to use StateContainer in MAUIOctober 7, 2024

    interesting post _________________ слоты в 1xbet приложение

  4. Luke on How to use drag and drop gesture to CollectionView in MAUISeptember 25, 2024

    Thanks very much!

  5. Tony - Nhan Nguyen Lhoa Minh on How to apply item reordering to CollectionView GridItemsLayout in MAUIMarch 28, 2024

    That would be an amazing solution. It's easy to comprehend. Thank you very much.

Our Visitor

0 0 8 1 0 4
Users Today : 10
Users Yesterday : 12
Users Last 7 days : 45
Users Last 30 days : 134
Users This Month : 109
Total views : 14383
How to check internet connection continuously in Xamarin Forms
Trending
How to check internet connection continuously in Xamarin Forms

Category

  • .Net MAUI
  • MAUI Community Toolkit
  • MVVM
  • Xamarin Community Toolkit
  • Xamarin Forms

Contact Me

  • LinkedIn
      © 2025 Learn Mobile Development