安卓Facebook应用程序:FB网络视图(登录网络视图)cookie的有效期是多久



我目前正在使用官方的android FB sdk开发一款android应用程序:因此,从身份验证/授权(SSO)到API调用,一切都是通过android FB sdk进行的。

但出于安全原因,我不想将access_token存储在手机上。现在,我有一个关于Facebookwebview存储的cookie的小问题(当您第一次登录时)。在退出android应用程序或终止进程后,似乎即使我没有在设备上存储access_token,我仍然可以在不提供凭据的情况下访问android应用程序,所以这可能是cookie。

你知道脸书的cookie有效期有多长吗?

谢谢。。。

您可能需要在完成cookie后手动删除它。

CookieManager.getInstance().removeAllCookies();

这可能是对所有cookie的攻击,所以如果你想对哪些cookie被删除进行更具体的控制,请查看文档。

http://developer.android.com/reference/android/webkit/CookieManager.html

以下是Facebook自己清除cookie的实现。

    public static void clearFacebookCookies(Context context) {
        // setCookie acts differently when trying to expire cookies between builds of Android that are using
        // Chromium HTTP stack and those that are not. Using both of these domains to ensure it works on both.
        clearCookiesForDomain(context, "facebook.com");
        clearCookiesForDomain(context, ".facebook.com");
        clearCookiesForDomain(context, "https://facebook.com");
        clearCookiesForDomain(context, "https://.facebook.com");
    }

private static void clearCookiesForDomain(Context context, String domain) {
        // This is to work around a bug where CookieManager may fail to instantiate if CookieSyncManager
        // has never been created.
        CookieSyncManager syncManager = CookieSyncManager.createInstance(context);
        syncManager.sync();
        CookieManager cookieManager = CookieManager.getInstance();
        String cookies = cookieManager.getCookie(domain);
        if (cookies == null) {
            return;
        }
        String[] splitCookies = cookies.split(";");
        for (String cookie : splitCookies) {
            String[] cookieParts = cookie.split("=");
            if (cookieParts.length > 0) {
                String newCookie = cookieParts[0].trim() + "=;expires=Sat, 1 Jan 2000 00:00:01 UTC;";
                cookieManager.setCookie(domain, newCookie);
            }
        }
        cookieManager.removeExpiredCookie();
    }

如果你有Facebook SDK作为lib连接到你的项目。只需调用Session.getActiveSession().clearFacebookCookies(getApplicationContext())

最新更新