繁体   English   中英

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

[英]How do I set the cookie on the WebView2 control using the CoreWebView2CookieManager

提前致谢。

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.

这是我在 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);

}

只是想知道我在这里是否缺少任何东西? 我是否还需要使用 WebResourceRequested 事件来设置 cookie? 任何线索或例子将不胜感激。

问题是您将 cookie 域设置为顶级域.me 这是不合法的,出于安全原因,浏览器不会发送它。

想象一下,如果你将它设置为.com那么它会将 cookie 发送到世界的一半。 所以这是禁止的。

而是将其设置为.dnndev.me - 然后它将被发送到您的域。

您还设置: cookie.IsSecure = true; - 那么它只会被发送到https请求。 将其设置为false以包含http请求。

还将path设置为/ - 然后它将发送到您服务器上的所有路径。

暂无
暂无

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

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