Xamarin Android CookieManager不会存储所有cookie [英] Xamarin Android CookieManager doesn't store all cookies

查看:279
本文介绍了Xamarin Android CookieManager不会存储所有cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



它返回两个cookie。



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

 公共重写void OnPageFinished(WebView view,string url)
{
base.OnPageFinished(view,url);
var handler = OnPageCompleted;
var uri = new Uri(url);
AllowCookies(视图);
var cookies = CookieManager.Instance.GetCookie(url);
var onPageCompletedEventArgs = new OnPageCompletedEventArgs {Cookies = Cookies,Url = uri.AbsolutePath,RelativeUrl = uri.PathAndQuery,Host = uri.Host};
handler?.Invoke(this,onPageCompletedEventArgs);
}
private void AllowCookies(WebView view)
{
CookieManager.Instance.Flush();
CookieManager.AllowFileSchemeCookies();
CookieManager.SetAcceptFileSchemeCookies(true);
CookieManager.Instance.AcceptCookie();
CookieManager.Instance.AcceptThirdPartyCookies(view);
CookieManager.Instance.SetAcceptCookie(true);
CookieManager.Instance.SetAcceptThirdPartyCookies(view,true);
}

问题是,我只能得到一个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还必须设置其 path属性,以便在从cookie存储中检索cookie之前可以应用路径匹配规则。
因为我的两个cookie都具有不同的路径,是因为没有出现将路径设置为 / project的cookie?

解决方案

经过几天又几天找到问题的答案。我终于找到了答案。
我使用桌面镶边对webview进行了远程调试,发现我需要的所有cookie都存在于webview中。
但是方法,

  var cookies = CookieManager.Instance.GetCookie(url); 

不返回具有相同网站变量集的Cookie。
这似乎是Xamarin Android的错误。我已经在 Xamarin Android github中提出了一个问题。



在xamarin android github问题中,我提到了重现步骤。
对我来说,解决此问题的解决方法是在我的asp.net核心后端项目中设置samesite cookie varibale。
如下:



为了在使用Identity时配置应用程序cookie,可以在启动公司的ConfigureServices中使用ConfigureApplicationCookie方法:

  //添加身份
服务。AddIdentity< ApplicationUser,IdentityRole>();

//配置应用程序cookie
services.ConfigureApplicationCookie(options =>
{
options.Cookie.SameSite = SameSiteMode.None;
} );

上述解决方案的链接。 此处。 p>

I am using Android Web View in my Xamarin Project to perform third party authentication. Once the login is successful I need to extract the authentication cookies. This cookies I am storing in persistent storage and then I am using them for passing to subsequent requests. For example:

Android App >(opens) webview > Loads (idp provider) url > User provides credentials and saml request is sent to my backend server > backend server validates saml and returns authentication cookies.

It returns two cookies.

Now everything works fine. And in OnPageFinished method of the WebClient of webview I am trying to extract the cookies using the method.

public override void OnPageFinished(WebView view, string url)
    {
        base.OnPageFinished(view, url);
        var handler = OnPageCompleted;
        var uri = new Uri(url);
        AllowCookies(view);
        var cookies = CookieManager.Instance.GetCookie(url);
        var onPageCompletedEventArgs = new OnPageCompletedEventArgs { Cookies = cookies, Url = uri.AbsolutePath, RelativeUrl = uri.PathAndQuery, Host = uri.Host };
        handler?.Invoke(this, onPageCompletedEventArgs);
    }
private void AllowCookies(WebView view)
    {
        CookieManager.Instance.Flush();
        CookieManager.AllowFileSchemeCookies();
        CookieManager.SetAcceptFileSchemeCookies(true);
        CookieManager.Instance.AcceptCookie();
        CookieManager.Instance.AcceptThirdPartyCookies(view);
        CookieManager.Instance.SetAcceptCookie(true);
        CookieManager.Instance.SetAcceptThirdPartyCookies(view, true);
    }

The problem is, I am able to get just one cookie(wc_cookie_ps_ck ), I am unable to see the other authentication cookie(.AspNetCore.Cookies ). Here's how the cookies appear in browser.

Please note that in postman and in chrome browser both the cookies appear. But in android webview only cookie with name ".AspNetCore.Cookies" is not appearing at all.

As per Java document,"When retrieving cookies from the cookie store, CookieManager also enforces the path-match rule from section 3.3.4 of RFC 2965 . So, a cookie must also have its "path" attribute set so that the path-match rule can be applied before the cookie is retrieved from the cookie store." Since both of my cookies have different path, is that the reason the one with path set as "/project" is not appearing?

解决方案

After days and days of finding the answer to the question. I finally have found an answer. I did remote debugging of the webview with the desktop chrome and I found out that all the cookies that I needed were present in the webview. However the method,

var cookies = CookieManager.Instance.GetCookie(url);

doesn't return the cookie which has the same site variable set. This looks like a bug from Xamarin Android. I have already raised an issue in Xamarin Android github.

In the xamarin android github issue I have mentioned the steps to reproduce. For me, the workaround to resolve the issue was to set the samesite cookie varibale off in my asp.net core back end project. As follows:

In order to configure the application cookie when using Identity, you can use the ConfigureApplicationCookie method inside your Startup’s ConfigureServices:

// add identity
services.AddIdentity<ApplicationUser, IdentityRole>();

// configure the application cookie
services.ConfigureApplicationCookie(options =>
{
    options.Cookie.SameSite = SameSiteMode.None;
});

Link for the above solution mentioned. Here.

这篇关于Xamarin Android CookieManager不会存储所有cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆