I am running Edge WebView2 with some login page loaded there. I want to make an automatic login.
To do that I need to remove current cookies. I can’t do that via js because they are httponly. For Edge beta browser I wrote simple Chrome extension for deleting cookies, but I can’t run an extension in WebView2 (or can?).
Also I know where the WebView2 cookies file is situated but I can’t change it while Webview is running.
The only way to do that in WebView is open DevTools, I removed them in application tab.
Any ideas on how to delete that cookies?
I would appreciate at least example of WebView2 page loading with custom header (where I can specify cookies) in c++.
Update: There is now an official ICoreWebView2CookieManager that can be used for managing cookies. Microsoft documents this API extremely well -- so its best to check their documentation.
With this new API, its just a matter of calling either
DeleteCookie
to delete a single cookie,DeleteCookies
to remove all cookies from a domain, orDeleteAllCookies
to clear all cookies under the same profile.(The original answer is retained below)
WebView2 is still in active development, and does not yet have a cookies API -- although it is a request that they are aware of.
The currently recommended approach to clearing/deleting cookies is to use
ICoreWebView2::CallDevToolsProtocolMethod
and issue aNetwork
command. This is also what Microsoft demonstrates in their sample browser application to delete all cookies. Using theDevTools
API will still work, even if front-end UI devtools are not enabled in the application.The parameters supplied to the command must be in JSON format, so if you want to delete a specific cookie using
Network.deleteCookies
, you will need to supply{"name":"<cookie name>;"}
to delete<cookie name>
:Or alternatively you can delete all cookies with
Network.clearBrowserCookies
:Note: The
CallDevToolsProtocolMethod
is issued asynchronously, and so if you may need to supply a handler argument if you are needing the cookie deleted before proceeding.