问题标签 [android-cookiemanager]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
440 浏览

android - VM 范围的 cookie 管理是什么意思?

我正在学习cookies在 Android 中存储,并遇到了几种实现它的方法。其中之一是使用CookieManager and CookieStore.

在浏览 Android 文档时,我遇到了以下声明:

为了在客户端和服务器之间建立和维护一个潜在的长期会话,HttpURLConnection 包括一个可扩展的 cookie 管理器。使用 CookieHandler 和 CookieManager 启用 VM 范围的 cookie 管理:

我不明白 的意思VM-wide cookie management。我知道 VM 的意思是Virtual Machine.

我的解释:

  1. 我解释它的一种方式是,创建一个CookieManager并将其传递给它以setDefault()使其在整个应用程序中可用。因此,我尝试了以下方法来测试它。

    我设置cks and cks1为并按预期TextViews打印cookie 。content/value根据我的解释,我尝试cookieManager.getCookieStore().getCookies().get(0).getValue();了另一个活动,但它无法识别该对象,这意味着它超出了范围且无法访问。此外,创建了一个新的CookieManager并试图获取 cookie 但它返回了null。因此,我认为这种对跨活动可访问 VM 范围的解释是不正确的。

  2. 第二个解释是 Cookies 会在CookieManager设置时自动存储。我从另一个关于 SO 的问题的解决方案中得到了它:Cookie management with Java URLConnection

解决方案中的陈述之一是这样建议的:

当 HttpURLConnection 接收到来自服务器的 cookie 时,CookieManager 将接收 cookie 并存储它。未来对同一服务器的请求将自动发送先前设置的 cookie。

我删除cookieManager.getCookieStore().add(uri,hc);以测试它,发现 cookie 不会自动存储。所以,这种解释也失败了。

另一个困扰我的疑问:

大多数存储 cookie 以供以后使用的解决方案都建议使用SharedPreferences. 困扰我的事情是,它们CookieManager最初都将 cookie 存储在其中,然后将其移至SharedPreferences. 为什么不SharedPreferences直接使用?

例如:

那么使用CookieManager然后移动它有什么意义SharedPreferences呢?

0 投票
0 回答
534 浏览

cookies - Android WebView获取原始cookie信息

我尝试在 Android WebView 中使用 CookieManager 获取 cookie 信息。然而,它所做的只是提供一个 cookie 的键值对,没有额外的信息。

我还尝试了这篇文章并使用它connection.getHeaderFields.get("Set-Cookie");来获取从服务器接收到的所有 cookie。但据我所知,我没有看到我应该从服务器接收的所有 cookie。这也可以通过 CookieManger 中的某些 cookie 接收新值但未显示在Set-Cookie字段中来发现。

理想情况下,我想拦截每个响应并使用来自服务器的所有信息(例如名称、值、最大年龄、安全等)检查他们的 cookie。有什么建议如何使用 Android WebView (WebViewClient) 执行此操作吗?

0 投票
0 回答
22 浏览

javascript - Android重定向到另一个页面

我试图在成功认证后重定向我的用户(使用 AJAX)。我试过使用window.location.href 和 document.location但没有成功。我也想保留饼干。

这是我的 Android 应用程序代码:

0 投票
1 回答
1959 浏览

android - Xamarin Android CookieManager 不存储所有 cookie

我在我的 Xamarin 项目中使用 Android Web View 来执行第三方身份验证。登录成功后,我需要提取身份验证 cookie。我将这些 cookie 存储在持久存储中,然后使用它们传递给后续请求。例如:

Android App >(打开)webview > 加载(idp 提供者)url > 用户提供凭据并将 saml 请求发送到我的后端服务器 > 后端服务器验证 saml 并返回身份验证 cookie。

它返回两个 cookie。

现在一切正常。在 webview 的 WebClient 的 OnPageFinished 方法中,我试图使用该方法提取 cookie。

问题是,我只能得到一个 cookie(wc_cookie_ps_ck),我看不到另一个身份验证 cookie(.AspNetCore.Cookies)。以下是 cookie 在浏览器中的显示方式。

在此处输入图像描述

请注意,在邮递员chrome 浏览器中,cookie 都会出现。但在 android webview 中,只有名称为“.AspNetCore.Cookies”的 cookie 根本没有出现。

根据 Java 文档,“当从 cookie 存储中检索 cookie 时,CookieManager 还强制执行 RFC 2965 的第 3.3.4 节中的路径匹配规则。因此,cookie 还必须设置其“路径”属性,以便路径匹配可以在从 cookie 存储中检索 cookie 之前应用规则。 ” 由于我的两个 cookie 具有不同的路径,是路径设置为“/project”的那个没有出现的原因吗?

0 投票
0 回答
240 浏览

android - Cookies injected in a cookie manager are not always accessible to web views

I have a class that observes a user account. It starts observing it when the app starts. When the account changes, it sets the cookie in a cookie manager with setCookie.

When I start the app and the account is already there, it properly injects the cookie - when I open the web view I see it there.

When I start the app and the account is not there, nothing happens (that's expected), then I log in (this creates the account) and the code for injecting cookies is called (the callback of the setCookie says that the cookie was correctly injected). But when I open the web view, I do not see the cookie there. If I restart the app everything works again.

I tried many different configurations of web views and cookie manager. I tried call flush on a cookie manager. I tried setting acceptCookies, acceptThirdPartyCookies. On my web view I need to have javascript enabled and dom storage enabled.

This is how I set cookies:

I expected my cookies to be visible in web views immediately after the setCookie prints true but that's not happening in the cases when the user opens the app and the account is not there. Once the user logs in and the setCookie code is called it still doesn't work. But after restarting the app, everything works.

Almost like something at the beginning of the app prevents the app to set cookies. It works when I do it very early in the app lifecycle but not later.

Any ideas what could be the problem?

0 投票
2 回答
1511 浏览

android - 如何清理在 WebView 中创建的会话?

我有一个WebView实例,它允许用户进行身份验证并访问 Web 应用程序提供的某些部分功能。一旦用户完成了他们的工作,WebView为了安全起见,我们需要清理会话。

奇怪的是,下次我打开屏幕时WebView,会话仍然存在,尽管我尝试清理会话,但我可以从中断的地方继续。

我尝试做的事情(我实际上运行了调试器以确保调用这些方法):

flush()removeAllCookies()'s 回调中调用的原因是该removeAllCookies()方法是异步的,所以我在想可能需要一些时间才能将内存中 cookie 存储的状态与持久存储同步,但这无济于事。

我没有做任何花哨的事情CookieManager,它是操作系统提供的默认设置(Android 9.0 安装在 Pixel 2 XL 上)。有什么我想念的吗?

0 投票
0 回答
144 浏览

android - 从 android webview 获取会话 cookie

我目前正在通过inappbrowser 开发一个带有ADFS 身份验证的cordova 应用程序。

到目前为止,这运作良好。但是我需要检索存储在 webview 实例中的会话 cookie,并能够在具有相同机制的另一个应用程序中使用它,因此用户不需要登录两次,因为两个应用程序都使用相同的公司登录名。

我试过用 CookieManager 恢复 cookie

但是,它没有列出会话 cookie。似乎 CookieManager 不会返回那些没有过期日期的 cookie。我已经能够通过运行成功终止会话

所以我确定会话 cookie 在那里,但我无法获取/操作它。

0 投票
0 回答
81 浏览

java - 在 Android 本机应用程序上保存持久性 cookie

在为浏览器开发时,cookie 是无缝的。但是,正如我发现的那样,在为 android 开发本机 java 应用程序时,这是一个全新的故事。

在 android 上存储持久性cookie的最佳方式是什么?

我将会话和常规 cookie 从服务器发送到应用程序。当应用程序关闭/手机重新启动时,我需要会话 cookie 消失,但我需要保留其他 cookie。就像现在一样,cookies 在内存中,在手机重启后就消失了。

请指教。

谢谢

0 投票
1 回答
28 浏览

java - Cookiemanager 根据 API 返回不同的 cookie 参数

我正在尝试使用CookieManager.getInstance().getCookie(url). 我注意到的是,对于 Android 10,cookie 包含__Host-nc_sameSiteCookielax=true; __Host-nc_sameSiteCookiestrict=true但是对于较低的 API,这些参数丢失了。差异来自哪里?

在我的情况下,这些参数是绝对必要的,所以现在我手动添加以获取更多请求。有没有更好的方法来解决这个问题?

0 投票
1 回答
1646 浏览

android - 在 Android 中退出应用程序后如何在 WebView 中保持会话?

我目前是 Android 开发的新手,但我正在尝试创建一个简单的 Android 应用程序来打开我的网站并允许您在 WebView 中登录它。但是,我意识到当您退出应用程序并终止其进程并重新启动应用程序时,它会要求您重新登录,而不是保持登录状态。我不知道如何解决这个问题。我已经研究了 CookieManager 的实现,但是,我无法让它正常工作。你能帮我看看我做错了什么吗?