You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CoreWebView2.CookieManager.AddOrUpdateCookie失效,指定URL下Domain与Path该如何设置?

CoreWebView2 CookieManager 设置无效的解决方案

针对目标URL http://ob-341.dnndev.com/identityserver/core,正确设置Domain和Path参数是让Cookie生效的关键,具体规则如下:

  • Domain参数:必须设置为目标站点的域名 ob-341.dnndev.com,不要包含协议(http://)或路径部分。这个设置能确保Cookie绑定到该域名,WebView2访问目标站点时会自动匹配并携带该Cookie。

  • Path参数:根据Cookie需要生效的范围选择:

    • 仅在 /identityserver/core 路径及子路径生效:设置为 /identityserver/core
    • 在 /identityserver 下所有路径生效:设置为 /identityserver/
    • 在整个域名下所有路径生效:设置为 /

修改后的代码示例:

// 目标站点域名
string cookieDomain = "ob-341.dnndev.com";
// 这里以全站生效的路径为例,可按需调整
string cookiePath = "/";

var coreWebView2Cookie = _webView2.WebView2.CoreWebView2.CookieManager.CreateCookie(
   name: cookie.Name, 
   value: cookie.Value, 
   Domain: cookieDomain, 
   Path: cookiePath); 
_webView2.WebView2.CoreWebView2.CookieManager.AddOrUpdateCookie(coreWebView2Cookie);

额外注意:

  • 必须在WebView2的CoreWebView2对象初始化完成后再执行Cookie设置操作,否则会导致无效或空引用错误。
  • Cookie的名称和值需遵循HTTP Cookie标准,避免包含非法特殊字符。

内容的提问来源于stack exchange,提问作者Prasanth Dummy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 09:02:24