英文:
Clear Cookies for my WebView2 Instance in C#
问题
我有一个小应用程序,可以登录到一个网站并执行一些操作。我需要一种清除Cookies并从网站中"强制"注销的方法,以便我可以再次执行操作。最简单的方法是清除Cookies,然后重复登录过程,但我还没有找到如何做到这一点。我找到了微软的这个资源,但还没有找到任何示例代码:
我认为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
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论