【问题标题】:Clear Cookies for my WebView2 Instance in C#在 C# 中清除我的 WebView2 实例的 Cookie
【发布时间】:2023-02-10 08:57:49
【问题描述】:
标签:
c#
cookies
webview
webview2
【解决方案1】:
如果您有 WebView2 类,您可以执行以下操作来清除 cookie 并重新加载页面:
// Clear cookies for this WebView2 and all WebView2s
// sharing the same user data folder.
webview2.CoreWebView2.CookieManager.DeleteAllCookies();
// Reload the document after clearing cookies
webview2.CoreWebView2.Reload();
上面的代码必须在 WebView2 完成初始化其 CoreWebView2 之后运行。
当您创建 CoreWebView2 时,它属于用户数据文件夹和浏览器进程。通过 CookieManager 更改 cookie 适用于共享相同配置文件和该用户数据文件夹的所有 CoreWebView2。
您不能在 WebView2 和浏览器之间共享状态,因此您不会在浏览器中修改 cookie。