/xamarin-fingerprint

Xamarin and MvvMCross plugin for authenticate a user via fingerprint sensor

Primary LanguageC#Apache License 2.0Apache-2.0

Fingerprint plugin for Xamarin

NuGet ![NuGet MvvMCross](https://img.shields.io/nuget/v/SMS.MvvmCross.Fingerprint.svg?label=NuGet MvvMCross)

Xamarin and MvvMCross plugin for accessing the fingerprint sensor.

Support & Limitations

Platform Version Limitations
Xamarin.Android 6.0
Xamarin.iOS 8.0 Cancelable programmatically since iOS 9.0
Windows UWP 10

Usage

API

The API is defined by the IFingerprint interface:

/// <summary>
/// Checks the availability of fingerprint authentication.
/// Possible Reasons for unavailability:
/// - Device has no sensor
/// - OS does not support this functionality
/// - Fingerprint is not enrolled
/// </summary>
bool IsAvailable { get; }

/// <summary>
/// Requests the authentication.
/// </summary>
/// <param name="reason">Reason for the fingerprint authentication request. Displayed to the user.</param>
/// <returns>Authentication result</returns>
Task<FingerprintAuthenticationResult> AuthenticateAsync(string reason);

/// <summary>
/// Requests the authentication (cancelable).
/// </summary>
/// <see cref="AuthenticateAsync(string)"/>
Task<FingerprintAuthenticationResult> AuthenticateAsync(string reason, CancellationToken cancellationToken);

The returned FingerprintAuthenticationResult contains information about the authentication.

/// <summary>
/// Indicatates whether the authentication was successful or not.
/// </summary>
public bool Authenticated { get { return Status == FingerprintAuthenticationResultStatus.Succeeded; } }

/// <summary>
/// Detailed information of the authentication.
/// </summary>
public FingerprintAuthenticationResultStatus Status { get; set; }

/// <summary>
/// Reason for the unsucessful authentication.
/// </summary>
public string ErrorMessage { get; set; }

Example

vanilla

var result = await Fingerprint.Current.AuthenticateAsync("Prove you have fingers!");
if (result.Authenticated)
{
    // do secret stuff :)
}
else
{
    // not allowed to do secret stuff :(
}

using MvvMCross

var fpService = Mvx.Resolve<IFingerprint>(); // or use dependency injection and inject IFingerprint

var result = await fpService.AuthenticateAsync("Prove you have mvx fingers!");
if (result.Authenticated)
{
    // do secret stuff :)
}
else
{
    // not allowed to do secret stuff :(
}

iOS

Nothing special on iOS. You can't configure anything and the standard iOS Dialog will be shown.

Android

Setup

Request the permission in AndroidManifest.xml

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

Set the resolver of the current Activity

Skip this, if you use the MvvMCross Plugin or don't use the dialog.

We need the current activity to display the dialog. You can use the Current Activity Plugin from James Montemagno or implement your own functionality to retrieve the current activity. See Sample App for details.

Fingerprint.SetCurrentActivityResolver(() => CrossCurrentActivity.Current.Activity);

Configuration

You can disable the dialog on Android (e.g. show fingerprint icon within your activity).

Fingerprint.DialogEnabled = false;

If you don't like the default dialog, you can easily customize it. You have to inherit from FingerprintDialogFragment e.g. like:

public class MyCustomDialogFragment : FingerprintDialogFragment
{
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var view = base.OnCreateView(inflater, container, savedInstanceState);
        view.Background = new ColorDrawable(Color.Magenta); // make it fancyyyy :D
        return view;
    }
}

And somewhere in your code set your custom dialog fragment:

Fingerprint.SetDialogFragmentType<MyCustomDialogFragment>();

Testing on Simulators

iOS

Controlling the sensor on the iOS Simulator

With the Hardware menu you can

  • Toggle the enrollment status
  • Trigger valid ( M) and invalid ( N) fingerprint sensor events

Android

  • start the emulator (Android >= 6.0)
  • open the settings app
  • go to Security > Fingerprint, then follow the enrollment instructions
  • when it asks for touch
  • open command prompt
  • telnet 127.0.0.1 <emulator-id> (adb devices prints "emulator-<emulator-id>")
  • finger touch 1
  • finger touch 1

Sending fingerprint sensor events for testing the plugin can be done with the telnet commands, too.

Note for Windows users: You have to enable telnet: Programs and Features > Add Windows Feature > Telnet Client

Contribution

+