Ads2

Friday, 1 August 2025

Biometric Unlock in MAUI

How To Implement Biometric Unlock in MAUI

Use the Microsoft.Maui.ApplicationModel APIs with platform-native biometric services.

1. Add Microsoft.Maui.Essentials (already included in MAUI projects)

2. Check if biometrics are available

using Microsoft.Maui.ApplicationModel; var result = await BiometricPrompt.Default.RequestAsync(new BiometricPromptRequest { Title = "Unlock App", Description = "Use your face or fingerprint to unlock", }); if (result.Authenticated) { // Continue to app } else { // Handle failure }

This uses Face ID / Fingerprint / Windows Hello, depending on the device.

3. Run on App Launch or Protected Page


protected override async void OnAppearing() { base.OnAppearing(); var authResult = await AuthenticateUser(); if (!authResult) { await DisplayAlert("Access Denied", "Biometric authentication failed.", "OK"); System.Diagnostics.Process.GetCurrentProcess().Kill(); // Or navigate back } }

 Platform Notes

 Android:

  • Uses BiometricPrompt API (supports face, fingerprint, iris)

 iOS:

  • Uses Face ID or Touch ID, depending on hardware

 Windows:

  • Uses Windows Hello (face, fingerprint, PIN)

What It Does

  • Prompts user for biometric authentication on app start

  • Grants access if biometric is successful

  • Exits or denies access if authentication fails

Step-by-Step Setup

✅ 1. Create a new MAUI App


dotnet new maui -n BiometricUnlockApp cd BiometricUnlockApp

✅ 2. Install Required Package (optional for Android)

.NET MAUI includes most features out of the box. No extra packages are required for biometrics via Microsoft.Maui.ApplicationModel.


✅ 3. MainPage.xaml


<VerticalStackLayout Padding="30" Spacing="20"> <Label Text="Biometric Unlock Demo" FontSize="32" HorizontalOptions="Center" /> <Label x:Name="StatusLabel" Text="Authenticating..." FontSize="20" HorizontalOptions="Center" /> </VerticalStackLayout>

✅ 4. MainPage.xaml.cs


using Microsoft.Maui.Controls; using Microsoft.Maui.ApplicationModel; namespace BiometricUnlockApp; public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); } protected override async void OnAppearing() { base.OnAppearing(); var isAvailable = await BiometricPrompt.Default.IsSupportedAsync(); if (!isAvailable) { await DisplayAlert("Not Supported", "Biometric authentication is not available on this device.", "OK"); StatusLabel.Text = "Biometric not available"; return; } var result = await BiometricPrompt.Default.RequestAsync(new BiometricPromptRequest { Title = "Unlock App", Description = "Use your face, fingerprint, or PIN to unlock.", CancelTitle = "Cancel" }); if (result.Authenticated) { StatusLabel.Text = "Access granted ✅"; } else { StatusLabel.Text = "Access denied ❌"; await DisplayAlert("Authentication Failed", "You could not be authenticated.", "Exit"); System.Diagnostics.Process.GetCurrentProcess().Kill(); // Close the app } } }

 Platform-Specific Configuration

✅ Android: AndroidManifest.xml

Add these inside <manifest>:


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

✅ iOS: Info.plist

Add these inside <dict>:

<key>NSFaceIDUsageDescription</key> <string>We use Face ID to unlock the app securely</string>

✅ Windows: No config needed

Uses Windows Hello automatically.


✅ Run and Test

  • Android: Face unlock, fingerprint, or device credential (depending on device)

  • iOS: Face ID or Touch ID

  • Windows: Windows Hello (if available)

No comments:

Post a Comment

Fantasy Cricket Mobile App in .NET

  1. Planning the Fantasy Cricket App Key Features to Include: User registration and authentication Player selection interface Match schedul...