Esempio n. 1
0
 internal void SetFingerprintDialog(FingerprintDialog fingerprintDialog)
 {
     this._fingerprintDialog = fingerprintDialog;
 }
Esempio n. 2
0
        public static async System.Threading.Tasks.Task Authenticate(DialogConfiguration dialogConfiguration)
        {
            //Android 10+ Q SdkInt 29+
            if ((int)Build.VERSION.SdkInt >= 29)
            {
                CancellationSignal BiometricCancellationSignal = new CancellationSignal();
                //Android 10
                BiometricCancellationSignal = new CancellationSignal();
                BiometricPrompt biometricPrompt;
                if (dialogConfiguration.IsUseAlternativeAuthentication)
                {
                    //Allow using PIN / Passcode as alternative, will disable crypto in authentication
                    biometricPrompt = new BiometricPrompt
                                      .Builder(Configuration.CurrentActivity)
                                      .SetTitle(dialogConfiguration.DialogTitle)
                                      .SetDescription(dialogConfiguration.DialogDescription)
                                      .SetDeviceCredentialAllowed(true)
                                      .Build();
                }
                else
                {
                    biometricPrompt = new BiometricPrompt
                                      .Builder(Configuration.CurrentActivity)
                                      .SetTitle(dialogConfiguration.DialogTitle)
                                      .SetDescription(dialogConfiguration.DialogDescription)
                                      .SetNegativeButton(dialogConfiguration.AlternativeActionMessage, Configuration.CurrentActivity.MainExecutor, new NegativeButtonOnClickListener(() =>
                    {
                        BiometricCancellationSignal.Cancel();
                        dialogConfiguration.AlternativeAction?.Invoke();
                    }))
                                      .Build();
                }
                BiometricPrompt.AuthenticationCallback authenticationCallback = new BiometricAuthenticationCallback();
                AuthenticationResult = null;
                AuthenticationResult = (arg) =>
                {
                    string result = arg;
                    if (arg == SystemMessages.Success)
                    {
                        BiometricCancellationSignal.Cancel();
                        dialogConfiguration.SuccessAction?.Invoke();
                    }
                    else if (arg == SystemMessages.Error)
                    {
                        BiometricCancellationSignal.Cancel();
                        dialogConfiguration.FailedAction?.Invoke();
                    }
                };
                if (dialogConfiguration.IsUseAlternativeAuthentication)
                {
                    //Secret key is not allowed to use when SetDeviceCredentialAllowed is true
                    biometricPrompt.Authenticate(BiometricCancellationSignal, Configuration.CurrentActivity.MainExecutor, authenticationCallback);
                }
                else
                {
                    if (Configuration.IsUseSecretKey)
                    {
                        CryptoObjectHelper cryptoHelper = new CryptoObjectHelper();
                        biometricPrompt.Authenticate(cryptoHelper.BuildBiometricPromptCryptoObject(), BiometricCancellationSignal, Configuration.CurrentActivity.MainExecutor, authenticationCallback);
                    }
                    else
                    {
                        biometricPrompt.Authenticate(BiometricCancellationSignal, Configuration.CurrentActivity.MainExecutor, authenticationCallback);
                    }
                }
            }
            //Android 9+ P SdkInt 28
            else if ((int)Build.VERSION.SdkInt >= 28)
            {
                //Android 9
                try
                {
                    CancellationSignal BiometricCancellationSignal = new CancellationSignal();
                    BiometricPrompt    biometricPrompt;
                    biometricPrompt = new BiometricPrompt
                                      .Builder(Configuration.CurrentActivity)
                                      .SetTitle(dialogConfiguration.DialogTitle)
                                      .SetDescription(dialogConfiguration.DialogDescription)
                                      .SetNegativeButton(dialogConfiguration.AlternativeActionMessage, Configuration.CurrentActivity.MainExecutor, new NegativeButtonOnClickListener(() =>
                    {
                        BiometricCancellationSignal.Cancel();
                        dialogConfiguration.AlternativeAction?.Invoke();
                    }))
                                      .Build();

                    BiometricCancellationSignal = new CancellationSignal();

                    BiometricPrompt.AuthenticationCallback authenticationCallback = new BiometricAuthenticationCallback();
                    AuthenticationResult = null;
                    AuthenticationResult = (arg) =>
                    {
                        string result = arg;
                        if (arg == SystemMessages.Success)
                        {
                            BiometricCancellationSignal.Cancel();
                            dialogConfiguration.SuccessAction?.Invoke();
                        }
                        else if (arg == SystemMessages.Error)
                        {
                            BiometricCancellationSignal.Cancel();
                            dialogConfiguration.FailedAction?.Invoke();
                        }
                    };
                    if (Configuration.IsUseSecretKey)
                    {
                        CryptoObjectHelper cryptoHelper = new CryptoObjectHelper();
                        biometricPrompt.Authenticate(cryptoHelper.BuildBiometricPromptCryptoObject(), BiometricCancellationSignal, Configuration.CurrentActivity.MainExecutor, authenticationCallback);
                    }
                    else
                    {
                        biometricPrompt.Authenticate(BiometricCancellationSignal, Configuration.CurrentActivity.MainExecutor, authenticationCallback);
                    }
                }
                catch (Exception ex)
                {
                    Configuration.CurrentActivity.RunOnUiThread(() =>
                    {
                        Toast.MakeText(Configuration.CurrentActivity, $"Throw exception : {ex.Message}", ToastLength.Long).Show();
                    });
                }
            }
            //Android 6+ to 8.x SdkInt 23
            else if ((int)Build.VERSION.SdkInt >= 23)
            {
                //Android 6+ to 8.x
                FingerprintManagerCompat fingerprintManager = FingerprintManagerCompat.From(Configuration.CurrentActivity);
                const int          flags        = 0; /* always zero (0) */
                CryptoObjectHelper cryptoHelper = new CryptoObjectHelper();
                // Using the Support Library classes for maximum reach
                FingerprintManagerCompat fingerPrintManager = FingerprintManagerCompat.From(Configuration.CurrentActivity);
                // AuthCallbacks is a C# class defined elsewhere in code.

                Android.Support.V4.OS.CancellationSignal        FingerprintCancellationSignal = new Android.Support.V4.OS.CancellationSignal();
                FingerprintManagerCompat.AuthenticationCallback authenticationCallback        = new AuthenticationCallBack();

                Android.Support.V4.App.FragmentTransaction transcation = ((FragmentActivity)Configuration.CurrentActivity).SupportFragmentManager.BeginTransaction();
                FingerprintDialog fingerprintDialog = new FingerprintDialog(dialogConfiguration, authenticationCallback, FingerprintCancellationSignal);
                fingerprintDialog.Show(transcation, "Dialog Fragment");

                AuthenticationResult = null;
                AuthenticationResult = (arg) =>
                {
                    string result = arg;
                    if (arg == SystemMessages.Success)
                    {
                        FingerprintCancellationSignal.Cancel();
                        dialogConfiguration.SuccessAction?.Invoke();
                    }
                    else if (arg == SystemMessages.Error)
                    {
                        FingerprintCancellationSignal.Cancel();
                        dialogConfiguration.FailedAction?.Invoke();
                    }
                };
                try
                {
                    fingerprintManager.Authenticate(cryptoHelper.BuildFingerprintManagerCompatCryptoObject(), flags, FingerprintCancellationSignal, authenticationCallback, null);
                }
                catch (Exception ex)
                {
                    Toast.MakeText(Configuration.CurrentActivity, $"Throw exception : {ex.Message}", ToastLength.Long);
                }
            }
        }