Xamarin Forms - Access CookieStore from a WebView

827 Views Asked by At

I'm creating a login page to a website and I want to use the cookies retrieved from this site to perform actions automatically

So I followed this article to access cookies from a given website,

From sniffing the packets between my device and the server, I received this very SetCookie:

Set-Cookie: AppCookie=COOKIEVALUE; expires=Tue, 13 Jul 2021 21:35:00 GMT; path=/; secure; samesite=lax; httponly

Although after retrieveing the Cookies for the site I don't get this cookie

Could anyone think of a reason for that?

Thanks in advance.

1

There are 1 best solutions below

4
On

You could use custom renderer to retrieving the Cookies.

On Android:

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

enter image description here

You could download the source file from the GitHub: https://github.com/WendyZang/CookiesWebView

On iOS:

[assembly: ExportRenderer(typeof(WebView), typeof(CustomWebViewRenderer))]
namespace Sample.iOS
{
public class CustomWebViewRenderer : WkWebViewRenderer
{
    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            NavigationDelegate = new CustomNavigationDelegate();
        } 
    }
}

public class CustomNavigationDelegate : WKNavigationDelegate
{
    public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
    {
        webView.Configuration.WebsiteDataStore.HttpCookieStore.GetAllCookies((cookies) =>
        {

        });
    }
 }
}