KeiStory

.NET MAUI 지문 인증(Fingerprint) 사용하기

 

Nuget 패키지 관리에서 Plugin.Fingerprint 를 설치합니다.

 

MainPage.xaml.cs 에서 지문 인증 코드를 아래와 같이 추가합니다.

using Plugin.Fingerprint;
using Plugin.Fingerprint.Abstractions;

namespace Maui.BiometricTest
{
    public partial class MainPage : ContentPage
    {
        public MainPage(IFingerprint fingerprint)
        {
            InitializeComponent();
        }

        private async void OnClicked(object sender, EventArgs e)
        {
            var request = new AuthenticationRequestConfiguration("지문인증", "지문을 인식해주세요.");

            var result = await CrossFingerprint.Current.AuthenticateAsync(request);

            if (result.Authenticated)
            {
                await DisplayAlert("지문인식 성공!", "접근가능", "OK");
            }
            else
            {
                await DisplayAlert("지문인식 실패!", "접근불가", "OK");
            }
        }
    }
}

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="MauiApp5.MainPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

    <ScrollView>
        <VerticalStackLayout Padding="30,0" Spacing="25">
            <Label
                SemanticProperties.HeadingLevel="Level1"
                Style="{StaticResource Headline}"
                Text="Fingerprint" />

            <Button
                Clicked="OnClicked"
                HorizontalOptions="Fill"
                SemanticProperties.Hint="Counts the number of times you click"
                Text="Fingerprint" />
        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

 

MauiProgram.cs 에 아래 코드를 추가합니다.

builder.Services.AddSingleton<MainPage>();
builder.Services.AddSingleton(typeof(IFingerprint), CrossFingerprint.Current);

using Plugin.Fingerprint;
using Plugin.Fingerprint.Abstractions;

namespace Maui.BiometricTest
{
    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                });

            builder.Services.AddSingleton<MainPage>();
            builder.Services.AddSingleton(typeof(IFingerprint), CrossFingerprint.Current);

            return builder.Build();
        }
    }
}

 

Platforms > Android > MainActivity.cs 에  OnCreate override 메서드를 추가합니다.

using Android.App;
using Android.Content.PM;
using Android.OS;

using Plugin.Fingerprint;

namespace Maui.BiometricTest
{
    [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
    public class MainActivity : MauiAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            CrossFingerprint.SetCurrentActivityResolver(() => this);
        }
    }
}

 

Platforms > Android > AndroidManifest.xml 에 권한을 추가합니다.
<uses-permission android:name="android.permission.USE_BIOMETRIC" />

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
	<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.USE_BIOMETRIC" />
</manifest>

 

결과 (지문 인식 부분은 보안으로 검게 보여집니다.)

참고

https://github.com/smstuebe/xamarin-fingerprint

 

GitHub - smstuebe/xamarin-fingerprint: Xamarin and MvvMCross plugin for authenticate a user via fingerprint sensor

Xamarin and MvvMCross plugin for authenticate a user via fingerprint sensor - smstuebe/xamarin-fingerprint

github.com

 

반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band