清除 WebView2 实例中的 Cookies 在 C# 中

huangapple go评论83阅读模式
英文:

Clear Cookies for my WebView2 Instance in C#

问题

我有一个小应用程序,可以登录到一个网站并执行一些操作。我需要一种清除Cookies并从网站中"强制"注销的方法,以便我可以再次执行操作。最简单的方法是清除Cookies,然后重复登录过程,但我还没有找到如何做到这一点。我找到了微软的这个资源,但还没有找到任何示例代码:

https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2cookiemanager.deleteallcookies?view=webview2-dotnet-1.0.1462.37

我认为WebView2和Edge密切相关,我还想确保这样做不会清除他们在Edge上的主要浏览器的Cookies。

非常感谢任何帮助。

英文:

I have a small application that logins into a website and performs some actions. I need a method to clear the cookies and essentially "Force" logout of the website so I can perform the actions again. The easiest way is to just clear the cookies and then repeat the login process, but I haven't been able to figure out how. I have found this resource from Microsoft but I haven't been able to locate any sample code

https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2cookiemanager.deleteallcookies?view=webview2-dotnet-1.0.1462.37

I believe WebView2 and Edge are closely related and also want to make sure by doing this I do not end up clearing the cookies on their main browser if they use Edge

Any assistance is greatly appreciated

答案1

得分: 1

如果您有一个 WebView2 类,您可以按照以下方式清除 cookies 并重新加载页面:

// 清除此 WebView2 和所有共享相同用户数据文件夹的 WebView2 的 cookies。
webview2.CoreWebView2.CookieManager.DeleteAllCookies();
// 清除 cookies 后重新加载文档。
webview2.CoreWebView2.Reload();

上述代码必须在 WebView2 初始化其 CoreWebView2 后运行。

当您创建 CoreWebView2 时,它属于一个用户数据文件夹和浏览器进程。通过 CookieManager 更改 cookies 会影响共享相同配置文件和用户数据文件夹的所有 CoreWebView2。

您不能在 WebView2 和浏览器之间共享状态,因此您无法修改浏览器中的 cookies。

英文:

If you have a WebView2 class you can do the following to clear cookies and reload the page:

// 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();

The above code must be run after the WebView2 has finished initializing its CoreWebView2.

When you create a CoreWebView2 it belongs to a user data folder and browser process. Changing cookies via the CookieManager applies to all CoreWebView2s sharing the same profile and that user data folder.

You cannot share state between the WebView2 and the browser so you will not be modifying cookies in the browser.

答案2

得分: 0

How about using Private mode?
MSDN interface ICoreWebView2ControllerOptions

You can clear cookies only dispose instance.

英文:

How about using Private mode?
MSDN interface ICoreWebView2ControllerOptions

You can clear cookies only dispose instance.

huangapple
  • 本文由 发表于 2023年2月9日 03:17:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定