How to prevent choosePrivateKeyAlias dialog in android app?

会有一股神秘感。 提交于 2020-06-23 04:00:08

问题


I have an android app that call a secured website in a webview. The webview retrieve the certificate to give it to the website.

I have to use the KeyChain.choosePrivateKeyAlias(this, keyChainAliasCallback, null, null, null, -1, CERT_ALIAS); method, and this displays a dialog like this

I'd want to display this window only the first time the user uses the app, but I don't know if it's possible.

I saw about intercepting this with a device/owner profile. Does that mean that it should be on android work ? it's a little blurry to me.

Another solution would be to save certificate and private key retrieve the first time somewhere inaccessible by any other app nor the user. I think about SharedPreferences in private mode.

Am I wrong ?

Thanks for your anwers !


回答1:


I not sure about the good way to resolve this problem, but here is what I did that worked fine for me.

I checked a boolean variable in the preferences, and if false returned, I display the choosPrivateKeyAlias window. If true returned, I know that I have the permission to retrieve certificate directly, so no need to display popup.

boolean isGranted = prefs.getBoolean("MY_CERT", false);
if(!isGranted) {
        //Get cert and private key from internal android store
        KeyChainAliasCallback keyChainAliasCallback = new KeyChainAliasCallback() {
            @Override
            public void alias(@Nullable String s) {
                Log.d(TAG, "selected alias = " + s);
                SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
                editor.putBoolean("MY_CERT", true);
                editor.commit();
                retriveCertsTask.execute();
            }
        };
        KeyChain.choosePrivateKeyAlias(mActivity, keyChainAliasCallback, null, null, null, -1, CERT_ALIAS);
    } else {
        // Retrieve certs an private key
        retriveCertsTask.execute();
    }

Hope it helps...



来源:https://stackoverflow.com/questions/44974349/how-to-prevent-chooseprivatekeyalias-dialog-in-android-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!