示例#1
0
        private void ShowBiometricPrompt()
        {
            var executor        = ContextCompat.GetMainExecutor(this);
            var passwordStorage = new PasswordStorageManager(this);
            var callback        = new AuthenticationCallback();

            callback.Success += async(_, result) =>
            {
                string password;

                try
                {
                    password = passwordStorage.Fetch(result.CryptoObject.Cipher);
                }
                catch
                {
                    Toast.MakeText(this, Resource.String.genericError, ToastLength.Short);
                    return;
                }

                await AttemptUnlock(password);
            };

            callback.Failed += delegate
            {
                FocusPasswordText();
            };

            callback.Error += (_, result) =>
            {
                Toast.MakeText(this, result.Message, ToastLength.Short).Show();
                FocusPasswordText();
            };

            _prompt = new BiometricPrompt(this, executor, callback);

            var promptInfo = new BiometricPrompt.PromptInfo.Builder()
                             .SetTitle(GetString(Resource.String.unlock))
                             .SetSubtitle(GetString(Resource.String.unlockBiometricsMessage))
                             .SetNegativeButtonText(GetString(Resource.String.cancel))
                             .SetConfirmationRequired(false)
                             .SetAllowedAuthenticators(BiometricManager.Authenticators.BiometricStrong)
                             .Build();

            Cipher cipher;

            try
            {
                cipher = passwordStorage.GetDecryptionCipher();
            }
            catch
            {
                _canUseBiometrics            = false;
                _useBiometricsButton.Enabled = false;
                return;
            }

            _prompt.Authenticate(promptInfo, new BiometricPrompt.CryptoObject(cipher));
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activityLogin);

            var executor = ContextCompat.GetMainExecutor(this);
            var callback = new AuthenticationCallback();

            callback.Success += OnSuccess;
            callback.Error   += OnError;

            var prompt = new BiometricPrompt(this, executor, callback);

            var promptInfo = new BiometricPrompt.PromptInfo.Builder()
                             .SetTitle(GetString(Resource.String.login))
                             .SetSubtitle(GetString(Resource.String.loginMessage))
                             .SetDeviceCredentialAllowed(true)
                             .Build();

            prompt.Authenticate(promptInfo);
        }
        private void ShowBiometicPrompt(IBioAuthCompleted bioAuthCompleted)
        {
            var activity = (FragmentActivity)CrossCurrentActivity.Current.Activity;
            var executor = ContextCompat.GetMainExecutor(activity);

            var callback = new BiometricAuthenticationCallback
            {
                Success = (AndroidX.Biometric.BiometricPrompt.AuthenticationResult result) => {
                    try
                    {
                        bioAuthCompleted.OnCompleted(BioAuthStatus.SUCCESS);
                    }
                    catch (SignatureException)
                    {
                        throw new RuntimeException();
                    }
                },
                Failed = () => {
                    // TODO: Show error.
                    bioAuthCompleted.OnCompleted(BioAuthStatus.FAILED);
                },
                Help = (BiometricAcquiredStatus helpCode, ICharSequence helpString) => {
                    // TODO: What do we do here?
                }
            };

            //Create prompt info
            var promptInfo = new AndroidX.Biometric.BiometricPrompt.PromptInfo.Builder()
                             .SetTitle("Biometric login for my app")
                             .SetSubtitle("Log in using your biometric credential")
                             .SetNegativeButtonText("Use account password")
                             .Build();

            //Create prompt
            var biometricPrompt = new AndroidX.Biometric.BiometricPrompt(activity, executor, callback);

            //call Authenticate
            biometricPrompt.Authenticate(promptInfo);
        }