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, or DeleteAllCookies
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 a Network
command. This is also what Microsoft demonstrates in their sample browser application to delete all cookies. Using the DevTools
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>
:
m_view->CallDevToolsProtocolMethod(L"Network.deleteCookies", L"{\"name\": \"<cookie name>\";}", nullptr);
Or alternatively you can delete all cookies with Network.clearBrowserCookies
:
m_view->CallDevToolsProtocolMethod(L"Network.clearBrowserCookies", L"{}", nullptr);
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.