【问题标题】:iOS Biometric authentication: Try Password work only after the second biometric fail attemptiOS 生物识别身份验证:仅在第二次生物识别失败尝试后尝试密码工作
【发布时间】:2019-10-17 01:06:14
【问题描述】:

我是第一次在 iOS 上尝试生物认证。

我的 touch id 验证码运行良好。但如果 touch id 失败,我想使用设备 PIN 进行身份验证。但这仅在第二次触摸 ID 尝试失败后才有效。第一次,当它失败时,会显示一个带有“尝试密码”按钮的警报。但是当触摸它时,它并没有去屏幕输入设备密码,而是再次显示输入触摸ID警报。

现在,如果触摸 ID 再次失败并且我触摸了输入密码按钮。它会进入屏幕输入设备 PIN。

但为什么它第一次不起作用?来自 Apple 文档:

后备按钮最初是隐藏的。对于 Face ID,在第一个之后 认证尝试不成功,将提示用户尝试 再次面容 ID 或取消。后退按钮显示在 第二次失败的面容 ID 尝试。 对于 Touch ID,后备按钮 在第一次不成功的 Touch ID 尝试后显示。

我发现它可以与 google pay 等应用配合使用。我在这里做错了什么。

这是我的代码。

public partial class AuthenticationViewController : UIViewController
{

    private LAContext context;

    public AuthenticationViewController(IntPtr handle) : base(handle)
    {
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        if (UserDefaultsManager.RememberMe)
            TryAuthenticate();
        else
            AppDelegate.Instance.GotoLoginController();
    }

    private void TryAuthenticate()
    {
        context = new LAContext();
        NSError error = null;

        if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0) &&
            context.CanEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, out error)) {
            // Biometry is available on the device
            context.EvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics,
                "Unlock SmartFHR", HandleLAContextReplyHandler);
        } else {
            // Biometry is not available on the device
            if (error != null) {
                HandleLAContextReplyHandler(false, error);
            } else {
                TryDevicePinAuthentication(error);
            }
        }
    }

    private void TryDevicePinAuthentication(NSError error)
    {
        if (context.CanEvaluatePolicy(LAPolicy.DeviceOwnerAuthentication, out error)) {
            context.EvaluatePolicy(LAPolicy.DeviceOwnerAuthentication,
                "Unlock SmartFHR", HandleLAContextReplyHandler);
        }
    }

    private void HandleLAContextReplyHandler(bool success, NSError error)
    {
        DispatchQueue.MainQueue.DispatchAsync(() => {
            if (success) {
                ContinueAfterAuthSuccess();
                return;
            }
            switch (error.Code) {
                case (long)LAStatus.UserCancel:
                    AppDelegate.Instance.GotoLoginController(true);
                    break;
                case (long)LAStatus.UserFallback:
                case (long)LAStatus.BiometryNotEnrolled:
                case (long)LAStatus.BiometryNotAvailable:
                    TryDevicePinAuthentication(error);
                    break;
            }
        });
    }

    private void ContinueAfterAuthSuccess()
    {
        if (Storyboard.InstantiateViewController("SplashController") is SplashController vc)
            AppDelegate.Instance.Window.RootViewController = vc;
    }

}

当第一次 touch id 尝试失败并且我点击 Try Password 按钮时,我看到它调用了带有错误代码 LAStatus.UserFallback 的 HandleLAContextReplyHandler。

【问题讨论】:

    标签: ios xamarin.ios biometrics touch-id


    【解决方案1】:

    LAPolicyDeviceOwnerAuthentication 的文档说:

    如果 Touch ID 或 Face ID 可用、已注册且未禁用,则 用户首先被要求。

    所以当您使用TryDevicePinAuthentication 显示身份验证窗口时,它仍然会首先显示生物识别窗口。

    如果要用户输入密码进行认证,我觉得DeviceOwnerAuthentication就够了。

    private void TryAuthenticate()
    {
        context = new LAContext();
    
        // if Biometry is available on the device, it will show it first
        context.EvaluatePolicy(LAPolicy.DeviceOwnerAuthentication, "Unlock SmartFHR", HandleLAContextReplyHandler);
    }
    

    这样,您只需要处理用户取消本次认证的情况。因为它会在用户点击回退按钮时自动弹出密码输入窗口:

    private void HandleLAContextReplyHandler(bool success, NSError error)
    {
        DispatchQueue.MainQueue.DispatchAsync(() => {
            if (success)
            {
                ContinueAfterAuthSuccess();
                return;
            }
            switch (error.Code)
            {
                case (long)LAStatus.UserCancel:
                    AppDelegate.Instance.GotoLoginController(true);
                    break;
                default:
                    break;
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 2014-05-29
      相关资源
      最近更新 更多