简体   繁体   中英

How do I set the cookie on the WebView2 control using the CoreWebView2CookieManager

Thanks in advance.

I am trying to set the cookie on my WPF based WebView2 browser control using the CoreWebView2CookieManager (CookieManagement API from WebView2 latest SDK 1.0.705.50), but the cookie is not getting set on the browser.

This is my piece of code in WPF application:

private void ButtonNavigateToLocal(object sender, RoutedEventArgs e)
{
   string uri = @"http://www.dnndev.me/";
   var cookie = _webView.CoreWebView2.CookieManager.CreateCookie("TestCookie", "XJKDKD", ".me", null);
   cookie.IsHttpOnly = true;
   cookie.IsSecure = true;                
  _webView.CoreWebView2.CookieManager.AddOrUpdateCookie(cookie);           
  _webView.CoreWebView2.Navigate(uri);

}

Just wanted to know if there is anything I am missing here? Do i also need to use the WebResourceRequested event to set the cookie? Any clues or example will be much appreciated.

The problem is that you set cookie domain to a top level domain .me . That is not legal and browser won't send it for security reasons.

Just imagine if you set it to .com then it would send the cookie to half the world. So that's forbidden.

Instead set it to .dnndev.me - then it will be sent to your domain.

You also set: cookie.IsSecure = true; - then it will only be sent to https requests. Set that to false to include http requests.

Also set path to / - then it will be sent all paths on your server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM