指纹身份验证Fingerprint Authentication

本指南讨论如何向 Xamarin Android 应用程序添加在 Android 6.0 中引入的指纹身份验证。This guide discusses how to add fingerprint authentication, introduced in Android 6.0, to a Xamarin.Android application.

指纹身份验证概述Fingerprint Authentication Overview

Android 设备上的指纹扫描器到达为应用程序提供了用户身份验证的传统用户名/密码方法的替代方法。The arrival of fingerprint scanners on Android devices provides applications with an alternative to the traditional username/password method of user authentication. 使用指纹对用户进行身份验证,使应用程序能够结合使用比用户名和密码更少的安全性。The use of fingerprints to authenticate a user makes it possible for an application to incorporate security that is less intrusive than a username and password.

FingerprintManager Api 使用指纹扫描器来定位设备,并运行 API 级别23(Android 6.0)或更高版本。The FingerprintManager APIs target devices with a fingerprint scanner and are running API level 23 (Android 6.0) or higher. Api 位于 Android.Hardware.Fingerprints 命名空间中。The APIs are found in the Android.Hardware.Fingerprints namespace. Android 支持库 v4 提供适用于较旧版本 Android 的指纹 Api 版本。The Android Support Library v4 provides versions of the fingerprint APIs meant for older versions of Android. 可在 Android.Support.v4.Hardware.Fingerprint 命名空间中找到兼容性 Api,这些 Api 通过Xamarin NuGet 包分发。The compatibility APIs are found in the Android.Support.v4.Hardware.Fingerprint namespace, are distributed through the Xamarin.Android.Support.v4 NuGet package.

FingerprintManager (及其支持库对应的FingerprintManagerCompat)是使用指纹扫描硬件的主要类。The FingerprintManager (and its Support Library counterpart, FingerprintManagerCompat) is the primary class for using the fingerprint scanning hardware. 此类是围绕系统级服务的 Android SDK 包装器,用于管理与硬件本身的交互。This class is an Android SDK wrapper around the system level service that manages interactions with the hardware itself. 它负责启动指纹扫描器并响应扫描程序的反馈。It is responsible for starting the fingerprint scanner and for responding to feedback from the scanner. 此类的接口非常简单,只包含三个成员:This class has a fairly straightforward interface with only three members:

  • Authenticate – 此方法将初始化硬件扫描程序并在后台启动服务,等待用户扫描其指纹。Authenticate – This method will initialize the hardware scanner and start the service in the background, waiting for the user to scan their fingerprint.
  • 如果用户已向设备注册一个或多个指纹,则 EnrolledFingerprints – 此属性将返回 trueEnrolledFingerprints – This property will return true if the user has registered one or more fingerprints with the device.
  • HardwareDetected – 此属性用于确定设备是否支持指纹扫描。HardwareDetected – This property is used to determine if the device supports fingerprint scanning.

Android 应用程序使用 FingerprintManager.Authenticate 方法来启动指纹扫描器。The FingerprintManager.Authenticate method is used by an Android application to start the fingerprint scanner. 以下代码片段举例说明了如何使用支持库兼容性 Api 调用它:The following snippet is an example of how to invoke it using the Support Library compatibility APIs:

// context is any Android.Content.Context instance, typically the Activity 
FingerprintManagerCompat fingerprintManager = FingerprintManagerCompat.From(context);
fingerprintManager.Authenticate(FingerprintManager.CryptoObject crypto,
                                int flags,
                                CancellationSignal cancel,
                                FingerprintManagerCompat.AuthenticationCallback callback,
                                Handler handler
                               );

本指南将讨论如何使用 FingerprintManager Api 来增强使用指纹身份验证的 Android 应用程序。This guide will discuss how to use the FingerprintManager APIs to enhance an Android application with fingerprint authentication. 它将介绍如何实例化和创建 CryptoObject 来帮助保护指纹扫描器中的结果。It will cover how to instantiate and create a CryptoObject to help secure the results from the fingerprint scanner. 我们将检查应用程序如何从指纹扫描器 FingerprintManager.AuthenticationCallback 和响应反馈。We'll examine how an application should subclass FingerprintManager.AuthenticationCallback and respond to feedback from the fingerprint scanner. 最后,我们将了解如何在 Android 设备或模拟器上注册指纹,以及如何使用adb来模拟指纹扫描。Finally, we'll see how to enroll a fingerprint on an Android device or emulator and how to use adb to simulate a fingerprint scan.

要求Requirements

指纹身份验证需要 Android 6.0 (API 级别23)或更高版本,以及带有指纹扫描器的设备。Fingerprint Authentication requires Android 6.0 (API level 23) or higher and a device with a fingerprint scanner.

对于要进行身份验证的每个用户,指纹必须已注册到设备。A fingerprint must already be enrolled with the device for each user that is to be authenticated. 这涉及设置使用密码、PIN、刷模式或面部识别的屏幕锁。This involves setting up a screen lock that uses a password, PIN, swipe pattern, or facial recognition. 可以在 Android Emulator 中模拟某些指纹身份验证功能。It is possible to simulate some of the fingerprint authentication functionality in an Android Emulator. 有关这两个主题的详细信息,请参阅注册指纹部分。For more information on these two topics, please see the Enrolling a Fingerprint section.