WebView2Feedback icon indicating copy to clipboard operation
WebView2Feedback copied to clipboard

Need way to delete all cookies for URI and domain/path

Open viktoriya-lysenko opened this issue 2 years ago • 6 comments

Describe the feature/enhancement you need

We need an API for deleting all cookies for the URI and domain/path as a single call.

PS This was not implemented with https://github.com/MicrosoftEdge/WebView2Feedback/issues/84. As that one is very old I was asked to opened a new one. This is not a duplicate!

The scenario/use case where you would use this feature

When user logs off and we need to clear all cookies for particular URL without touching all the other cookies. There is a workaround which is first to get all the cookies for said URL and delete one by one, but performance is just horrible if there are a lot of cookies. We tried to 'optimize' it by deleting all the cookies, but that doesn't work very well, because user gets logged off from other servers and from SSO. Thousands of end users are impacted.

How important is this request to you?

Impactful. My app's user experience would be significantly compromised without it.

Suggested implementation

ICoreWebView2CookieManager has a function DeleteCookies, but it takes a name (of the cookie) as a parameter and this parameter is required. We would like to be able to put nullptr there to be able to delete all the cookies. There is also DeleteCookiesWithDomainAndPath function that has the same problem. If there will be different functions without a name as a parameter that would also work of cause.

What does your app do? Is there a pending deadline for this request?

We are add-in for MS Office that is used by thousands of users. We use WebView2 for authentication and now we are moving to using it for all the networking. In the last case the problem becomes very impactful. It would be great if we can have it implemented before December 1st.

AB#47528266

viktoriya-lysenko avatar Nov 09 '23 22:11 viktoriya-lysenko

Hi @viktoriya-lysenko, I have added your feature to our backlog. Thanks!

nishitha-burman avatar Nov 09 '23 23:11 nishitha-burman

What's the problem?

                string url = addressBar.Text.Trim();
                Uri urlx = new(url.ToLower());
                await WebView.CoreWebView2.CallDevToolsProtocolMethodAsync("Storage.clearDataForOrigin", "{\"origin\":\"" + urlx.Scheme + "://" + urlx.Host + "\",\"storageTypes\":\"all\"}");
                WebView.CoreWebView2.Navigate(url); // NOT RELOAD, need start new Navigate

leonidukg avatar Nov 11 '23 15:11 leonidukg

@leonidukg It's a nice workaround, but according to docs on dev tools (https://chromedevtools.github.io/devtools-protocol) Storage is not a part of Stable, it's tip-of-tree and can be broken any time.

viktoriya-lysenko avatar Nov 16 '23 21:11 viktoriya-lysenko

@leonidukg

I assure you, everything will be broken at some point. But this is a solution that works + to remove cookies from subdomains you can use the code and the full code will be like this:

                string url = addressBar.Text.Trim();
                Uri urlx = new(url.ToLower());
// Delete all domain data
                await WebView.CoreWebView2.CallDevToolsProtocolMethodAsync("Storage.clearDataForOrigin", "{\"origin\":\"" + urlx.Scheme + "://" + urlx.Host + "\",\"storageTypes\":\"all\"}");

// Delete all cookies on the *.site.com subdomain
                List<CoreWebView2Cookie> cookieList = await WebView.CoreWebView2.CookieManager.GetCookiesAsync(urlx.Scheme + "://." + urlx.Host);
                foreach (CoreWebView2Cookie cookie in cookieList)
                {
                    WebView.CoreWebView2.CookieManager.DeleteCookie(cookie);
                }
// You can't do a reload as all data will be restored, we need to create a new site load
                WebView.CoreWebView2.Navigate(url);

And while you wait for some features from the webview2 development team, it might be a couple of years, but it might not show up.

leonidukg avatar Nov 17 '23 07:11 leonidukg

@leonidukg - Saying everything will be broken is a bit of an exaggeration... This CDP interface has been stable for a long time, CDP just hasn't promoted anything from experimental to stable since Chromium 54 in October of 2016.

@viktoriya-lysenko - CDP is the recommended solution for this. With a pretty trivial workaround, this is unlikely to be something we'll prioritize any time soon.

aluhrs13 avatar Nov 30 '23 00:11 aluhrs13

not 100% sure this isn't an issue in my code, but the workaround above (clearDataForOrigin) used to work, and now it seems to not. Hopefully this isn't some kind of runtime regression in chromium/webview2.

pushkin- avatar Mar 25 '25 20:03 pushkin-