A unified API for biometry on iOS, Android, and Windows.
$ dotnet add package BiometryServiceThis library offers a simple contract to use the biometry across Android, iOS and Windows (UWP & WinUI).
Install BiometryService nuget package.
Get an IBiometryService instance.
IBiometryService is implemented by BiometryService.
The constructor of BiometryService is different on each platform.
On Windows there are no parameters.
_biometryService = new BiometryService();
On Android, you need to provide a fragmentActivity and a promptInfoBuilder.
var promptBuilder = () => new BiometricPrompt.PromptInfo.Builder()
.SetTitle("TODO: Title")
.SetSubtitle("TODO: Subtitle")
.SetNegativeButtonText("Cancel")
.SetAllowedAuthenticators(AndroidX.Biometric.BiometricManager.Authenticators.BiometricStrong)
.Build();
var biometryService = new BiometryService(
fragmentActivity: MainActivity.Instance,
promptInfoBuilder: promptBuilder,
loggerFactory: null
);
On iOS, you first need to set (key/value) in the file.
NSFaceIDUsageDescriptionInfo.plist <!-- info.plist -->
<key>NSFaceIDUsageDescription</key>
<string>TODO: Biometry would like to use Face Id</string>Then, instantiate of the service for iOS.
_biometryService = new BiometryService(
useOperationPrompt: "TODO: Subtitle",
laContext: null,
localAuthenticationPolicy: LAPolicy.DeviceOwnerAuthenticationWithBiometrics,
loggerFactory: null
);Use ScanBiometry to prompt the native experience.
This will use automaticaly use the native biometric service of that device (FaceID, TouchID, Android Fingerprint, ect.).
try
{
await _biometryService.ScanBiometry(CancellationToken.None);
// TODO: Handle the case when biometry is recognized.
}
catch (BiometryException biometryException)
{
// TODO: Handle the case when biometry is not recognized.
Console.WriteLine($"{biometryException.Reason} : {biometryException.Message}");
}
Face authentication is only available when using .SetAllowedAuthenticators(AndroidX.Biometric.BiometricManager.Authenticators.BiometricWeak) in the BiometricPrompt.PromptInfo.Builder instantiation that is required for the service. Please note that if you are using .SetAllowedAuthenticators(AndroidX.Biometric.BiometricManager.Authenticators.BiometricStrong) in the BiometricPrompt.PromptInfo.Builder Facial authentification is exclusively accessible on phones equipped with Class 3 Biometric capabilities. (Pixel 4 and 8 for now).
Please note that Encrypt and Decrypt methods are only available when using .SetAllowedAuthenticators(AndroidX.Biometric.BiometricManager.Authenticators.BiometricStrong) in the BiometricPrompt.PromptInfo.Builder instantiation that is required for the service.
Please also note that the prompt builder SetTitle and SetSubtitle are used for both Fingerprint and Face biometry. We suggest that you use something generic enough for both cases.
laContext parameter (local authentication context) can be set by creating a new LAContext.
var laContext = new LAContext
{
LocalizedReason = "This app wants to use biometry for ...",
LocalizedFallbackTitle = "Fallback Title",
LocalizedCancelTitle = "Cancel Title"
};
useOperationPrompt is only displayed on devices using TouchID.The IBiometryService has severals methods.
As of now, this is the list of features available per platform.
| Methods | iOS | Android | WinUI | UWP |
|---|---|---|---|---|
GetCapability | ✔ | ✔ | ✔ | ✔ |
ScanBiometry | ✔ | ✔ | ✔ | ✔ |
Encrypt | ✔ | ✔ | ✔ | ✔ |
Decrypt | ✔ | ✔ | ❌ | ❌ |
Remove | ✔ | ✔ | ✔ | ✔ |
Please note that in case of error, a BiometryException is thrown.
Biometry Exception Types:
Failed: Any other failures while trying to use the device biometrics.Unavailable: The device biometrics is not available.NotEnrolled: The device has not been enrolled to use biometrics.PasscodeNeeded: The passcode needs to be set on the device.Locked:
KeyInvalidated:
If it's a cancellation error, OperationCanceledException is thrown.
This method gets the device's current biometric capabilities.
It returns a struct BiometryCapabilities with the detailed device configuration.
This method attemps to scan the user's biometry.
await biometryService.ScanBiometry(CancellationToken.None);This method encrypts a value and stores it into the platform secure storage with the given key name.
await biometryService.Encrypt(CancellationToken.None, "KeyName", "KeyValue");On Android, a new CryptoObject from AndroidX.Biometric is created with a key as a parameter. Then the data is encrypted and presented to the BiometricPrompt manager.
The final step encodes the data in base64 and stores it in the shared preferences.
On iOS, the SecKeyChain is used to store a string linked to a key. The OS is in charge of securing the data with biometric authentication during the process.
This method decrypts and gets the data associated to the given key.
await biometryService.Decrypt(CancellationToken.None, "KeyName");On Android, the method retrieves the shared preference encrypted data, then decrypts it with the secret as a parameter by presenting it to the BiometricPrompt manager.
On iOS, the method retrieves the encrypted data from the SecKeyChain with the secret as a parameter. iOS is in charge of decrypting the data with biometric authentication during the process.
This method removes the ecrypted value from the platform secure storage.
biometryService.Remove("KeyName");On Android, the method removes the encrypted data from the shared preferences.
On iOS, the method removes the encrypted data from the SecKeyChain.
Please consult the BREAKING CHANGES for more information about breaking changes history.
This project is licensed under the Apache 2.0 license - see the LICENSE file for details.
Please read CONTRIBUTING.md for details on the process for contributing to this project.
Be mindful of our Code of Conduct.