2

I am using biometric Authentication method to unlock my app. But if user have disabled fingerprint or if user is using the device which does not support fingerprint, How can I use pin or pattern authentication for them.

I was using keyguardmanager before in 2018 and it worked for me that if user had no fingerprint setup or no fingerprint hardware available then the keyguard manager automatically asked for other default authentication method. I want to use same thing over here.

This is the code I am using in xamarin.android.

public void SetAuth()
    {
        try
        {
            KeyguardManager keyguardManager = (KeyguardManager)Activity.GetSystemService(Service.KeyguardService);
            BiometricPrompt biometricManager = new BiometricPrompt.Builder(Activity)
            .SetDescription("Authorization required to access data")// TODO: 
            .SetTitle("Access Data")// TODO:
            .SetNegativeButton("Cancel", Activity.MainExecutor, this)
            .SetSubtitle("").Build();//(BiometricPrompt)Activity.GetSystemService(Service.FingerprintService);
            if (ActivityCompat.CheckSelfPermission(Activity, Manifest.Permission.UseBiometric)
                != (int)Android.Content.PM.Permission.Granted)
            {
                ActivityCompat.RequestPermissions(Activity, new string[] { Manifest.Permission.UseBiometric }, 1);
                return; 
            }
            if (!keyguardManager.IsKeyguardSecure)
            {
                Helper.DialogBox(Activity, Activity.ApplicationContext, "Lock screen security not enable in Settings", null);
            }
            else
                GenKey();
            if (CipherInit())
            {
                BiometricPrompt.CryptoObject cryptoObject = new BiometricPrompt.CryptoObject(cipher);
                FingerprintHandler handler = new FingerprintHandler(Activity, this);
                handler.StartAuthentication(biometricManager, cryptoObject);
            }

        }
        catch (System.Exception ex)
        {
            ErrorLog(Params.Main_error, ex.Message + " :: " + ex.StackTrace, Params.AppInternalIssue_code);
        }

    }
private bool CipherInit()
        {
            try
            {
                cipher = Cipher.GetInstance(KeyProperties.KeyAlgorithmAes
                    + "/"
                    + KeyProperties.BlockModeCbc
                    + "/"
                    + KeyProperties.EncryptionPaddingPkcs7);
                keyStore.Load(null);
                IKey key = (IKey)keyStore.GetKey(KEY_NAME, null);
                cipher.Init(CipherMode.EncryptMode, key);
                return true;
            }
            catch (System.Exception ex) {
                return false;
            }
        }



private void GenKey()
        {
            keyStore = KeyStore.GetInstance("AndroidKeyStore");
            KeyGenerator keyGenerator = null;
            keyGenerator = KeyGenerator.GetInstance(KeyProperties.KeyAlgorithmAes, "AndroidKeyStore");
            keyStore.Load(null);
            keyGenerator.Init(new KeyGenParameterSpec.Builder(KEY_NAME, KeyStorePurpose.Encrypt | KeyStorePurpose.Decrypt)
                .SetBlockModes(KeyProperties.BlockModeCbc)
                .SetUserAuthenticationRequired(true)
                .SetEncryptionPaddings(KeyProperties
                .EncryptionPaddingPkcs7).Build());
            keyGenerator.GenerateKey();
        }
Zeeshan Shakil
  • 157
  • 2
  • 13
  • 1
    Here is a demo about how to use use device credentials (PIN, Pattern, Password) in your app. https://learn.microsoft.com/en-us/samples/xamarin/monodroid-samples/android-m-confirmcredential/ – Leon Apr 13 '20 at 08:53

0 Answers0