简体   繁体   English

如何使用 CoreWebView2CookieManager 在 WebView2 控件上设置 cookie

[英]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. 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:这是我在 WPF 应用程序中的一段代码:

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?我是否还需要使用 WebResourceRequested 事件来设置 cookie? Any clues or example will be much appreciated.任何线索或例子将不胜感激。

The problem is that you set cookie domain to a top level domain .me .问题是您将 cookie 域设置为顶级域.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.想象一下,如果你将它设置为.com那么它会将 cookie 发送到世界的一半。 So that's forbidden.所以这是禁止的。

Instead set it to .dnndev.me - then it will be sent to your domain.而是将其设置为.dnndev.me - 然后它将被发送到您的域。

You also set: cookie.IsSecure = true;您还设置: cookie.IsSecure = true; - then it will only be sent to https requests. - 那么它只会被发送到https请求。 Set that to false to include http requests.将其设置为false以包含http请求。

Also set path to / - then it will be sent all paths on your server.还将path设置为/ - 然后它将发送到您服务器上的所有路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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