mistelltein
mistelltein

Reputation: 121

Using Plugin.Fingerprint for Face ID Authentication in .NET MAUI

I'm working on a .NET MAUI project and trying to implement Face ID authentication using the Plugin.Fingerprint library. However, it seems the library prompts for fingerprint authentication instead of Face ID. Here's the code I have so far:

private async Task<bool> AuthenticateUsingFaceId()
{
    try
    {
        var authRequestConfig = new AuthenticationRequestConfiguration("Face ID Authentication", "Authenticate to enable face ID login")
        {
            AllowAlternativeAuthentication = false,
        };
        
        var isFaceIdAvailable = await Plugin.Fingerprint.CrossFingerprint.Current.IsAvailableAsync(true);
        if (!isFaceIdAvailable)
        {
            await Shell.Current.DisplayAlert("Error", "Face ID is not supported on this device", "OK");
            return false;
        }

        var result = await Plugin.Fingerprint.CrossFingerprint.Current.AuthenticateAsync(authRequestConfig, CancellationToken.None);
        if (!result.Authenticated)
        {
            await Shell.Current.DisplayAlert("Error", "Face ID authentication failed", "OK");
            return false;
        }

        var pin = await SecureStorage.GetAsync("saved_pin");
        var password = await SecureStorage.GetAsync("saved_password");

        if (string.IsNullOrEmpty(pin) || string.IsNullOrEmpty(password))
        {
            await Shell.Current.DisplayAlert("Error", "No saved credentials for face ID login", "OK");
            return false;
        }

        var tokenResponse = await _authenticationService.AuthenticateAsync(pin!, password!);
        if (tokenResponse == null)
        {
            await Shell.Current.DisplayAlert("Error", "Automatic login failed", "OK");
            return false;
        }

        await SecureStorage.SetAsync("access_token", tokenResponse.JwtToken!);
        await SecureStorage.SetAsync("refresh_token", tokenResponse.RefreshToken!);

        await Shell.Current.DisplayAlert("Success", "Logged in using face ID authentication", "OK");

        await Shell.Current.GoToAsync(nameof(NotificationPage));
        return true;
    }
    catch (Exception ex)
    {
        await Shell.Current.DisplayAlert("Error", ex.Message, "OK");
        return false;
    }
}

I have set AllowAlternativeAuthentication to false, hoping it would force Face ID authentication, but it still prompts for fingerprint. My understanding is that the library may not fully support Face ID.

Is there a way to configure Plugin.Fingerprint to prioritize Face ID over fingerprint authentication? Or do I have to use an alternative library or method to implement Face ID authentication in .NET MAUI?

Upvotes: 2

Views: 597

Answers (0)

Related Questions