Android Facebook应用程序:FB webview(登录webview)cookie有效多长时间?

时间:2012-02-24 16:30:39

标签: android facebook cookies access-token

我目前正在使用官方的android FB sdk开发一个Android应用程序:所以从身份验证/授权(SSO)到API调用的所有内容都是通过android FB sdk完成的。

但出于安全原因,我不想将 access_token 存储在手机上。现在我对 Facebook webview (首次登录时)存储的 cookie 有一点问题。退出Android应用程序或杀死进程后,似乎即使我没有在设备上存储access_token,我仍然可以访问Android应用程序而无需提供凭据,因此它可能是cookie。

你知道 facebook的cookie有效期多长时间?

谢谢......

2 个答案:

答案 0 :(得分:0)

完成后,您可能需要手动删除Cookie。

CookieManager.getInstance().removeAllCookies();

这可能是所有Cookie上的核武器,因此如果您想要更具体地控制哪些Cookie被删除,请查看文档。

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

答案 1 :(得分:0)

以下是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());