protected override async Task <FingerprintAuthenticationResult> NativeAuthenticateAsync(AuthenticationRequestConfiguration authRequestConfig, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(authRequestConfig.Title))
            {
                throw new ArgumentException("Title must not be null or empty on Android.", nameof(authRequestConfig.Title));
            }

            if (!(CrossFingerprint.CurrentActivity is FragmentActivity))
            {
                throw new InvalidOperationException($"Expected current activity to be '{typeof(FragmentActivity).FullName}' but was '{CrossFingerprint.CurrentActivity?.GetType().FullName}'. " +
                                                    "You need to use AndroidX. Have you installed Xamarin.AndroidX.Migration in your Android App project!?");
            }

            try
            {
                var cancel = string.IsNullOrWhiteSpace(authRequestConfig.CancelTitle) ?
                             Application.Context.GetString(Android.Resource.String.Cancel) :
                             authRequestConfig.CancelTitle;

                var handler = new AuthenticationHandler();
                var builder = new BiometricPrompt.PromptInfo.Builder()
                              .SetTitle(authRequestConfig.Title)
                              .SetConfirmationRequired(authRequestConfig.ConfirmationRequired)
                              .SetDescription(authRequestConfig.Reason);

                if (authRequestConfig.AllowAlternativeAuthentication)
                {
                    // It's not allowed to allow alternative auth & set the negative button
                    builder = builder.SetDeviceCredentialAllowed(authRequestConfig.AllowAlternativeAuthentication);
                }
                else
                {
                    builder = builder.SetNegativeButtonText(cancel);
                }
                var info     = builder.Build();
                var executor = Executors.NewSingleThreadExecutor();


                var activity = (FragmentActivity)CrossFingerprint.CurrentActivity;
                using var dialog = new BiometricPrompt(activity, executor, handler);
                await using (cancellationToken.Register(() => dialog.CancelAuthentication()))
                {
                    dialog.Authenticate(info);
                    var result = await handler.GetTask();

                    TryReleaseLifecycleObserver(activity, dialog);

                    return(result);
                }
            }
            catch (Exception e)
            {
                return(new FingerprintAuthenticationResult
                {
                    Status = FingerprintAuthenticationResultStatus.UnknownError,
                    ErrorMessage = e.Message
                });
            }
        }
Beispiel #2
0
 public void StopListening()
 {
     Kp2aLog.Log("Fingerprint: StopListening " + (_biometricPrompt != null ? " having prompt " : " without prompt"));
     _biometricAuthCallbackAdapter?.IgnoreNextError();
     _biometricPrompt?.CancelAuthentication();
 }
 public void StopListening()
 {
     _biometricAuthCallbackAdapter.IgnoreNextError();
     _biometricPrompt.CancelAuthentication();
 }
 public override void Dismiss()
 {
     base.Dismiss();
     _prompt?.CancelAuthentication();
 }