Search code examples
androidxamarincookiesxamarin.androidandroid-cookiemanager

Xamarin Android CookieManager doesn't store all cookies


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.

enter image description here

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?


Solution

  • 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.