Search code examples
cookieswebview2

Can't see Cookie set via CoreWebView2CookieManager unless blank domain used


I'm setting a cookie using CoreWebView2.CookieManager.AddOrUpdateCookie, example:

WebView2.Core.CoreWebView2Cookie cookie = webView21.CoreWebView2.CookieManager.CreateCookie("MyCookie", "123", "abc.com", "/");
cookie.IsHttpOnly = false;
cookie.IsSecure = true;               
webView21.CoreWebView2.CookieManager.AddOrUpdateCookie(cookie);

Now when I try and check if the cookie has been set using:

List<Microsoft.Web.WebView2.Core.CoreWebView2Cookie> cookieList = await webView21.CoreWebView2.CookieManager.GetCookiesAsync("abc.com");

I get no cookies returned, however if I change "abc.com" to "" in the .GetCookiesAsync("") I can see the cookie I created, so why am I not seeing the cookies even though I'm passing in the correct domain/filter. When passing in the blank domain filter, I see my cookie with the correct domain on it. What am I doing wrong here?


Solution

  • To get the cookie you must specify the full Url, not just the domain name.

    In your case, add https:// in front of the url.

    Reference: https://learn.microsoft.com/da-dk/dotnet/api/microsoft.web.webview2.core.corewebview2cookiemanager.getcookiesasync?view=webview2-dotnet-1.0.2849.39#microsoft-web-webview2-core-corewebview2cookiemanager-getcookiesasync(system-string)

    IMHO this is a bug, since cookies don't work with urls, they work with domains.