MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
449 stars 55 forks source link

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

Open viktoriya-lysenko opened 11 months ago

viktoriya-lysenko commented 11 months ago

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

nishitha-burman commented 11 months ago

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

leonidukg commented 11 months ago

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
viktoriya-lysenko commented 11 months ago

@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.

leonidukg commented 11 months ago

@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.

aluhrs13 commented 11 months ago

@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.