Android 如果未找到指纹,则使用Pin或模式进行身份验证

Android 如果未找到指纹,则使用Pin或模式进行身份验证,android,xamarin.android,android-fingerprint-api,android-biometric-prompt,Android,Xamarin.android,Android Fingerprint Api,Android Biometric Prompt,我正在使用生物认证方法解锁我的应用程序。但是,如果用户已禁用指纹,或者用户正在使用不支持指纹的设备,我如何为他们使用pin或模式身份验证 我在2018年之前使用过keyguardmanager,如果用户没有指纹设置或没有可用的指纹硬件,则keyguardmanager会自动请求其他默认身份验证方法,这对我来说很有用。我想在这里用同样的东西 这是我在xamarin.android中使用的代码 public void SetAuth() { try {

我正在使用生物认证方法解锁我的应用程序。但是,如果用户已禁用指纹,或者用户正在使用不支持指纹的设备,我如何为他们使用pin或模式身份验证

我在2018年之前使用过keyguardmanager,如果用户没有指纹设置或没有可用的指纹硬件,则keyguardmanager会自动请求其他默认身份验证方法,这对我来说很有用。我想在这里用同样的东西

这是我在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();
        }

下面是一个演示如何在应用程序中使用设备凭据(PIN、模式、密码)。