Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WinForms Example - File > Close Tab does not allow a fetch to clear cookies on page termination but CTRL + W hotkey does #4846

Open
1 task done
madaster97 opened this issue Jun 23, 2024 · 1 comment

Comments

@madaster97
Copy link

madaster97 commented Jun 23, 2024

Is there an existing issue for this?

  • I have searched both open/closed issues, no issue already exists.

CefSharp Version

v124.3.80

Operating System

Windows 10

Architecture

x64

.Net Version

net472

Implementation

WinForms

Reproduction Steps

In a CefSharp WinForms application hosting one of my company's web pages, we see an issue where closing a browser tab from the WinForms application will trigger page termination events but does not update the browser's cookie cache based on any fetch responses triggered during those events. For example, we register an unload handler like this (and the approach works in Chrome/Edge):

function serverHandlerUnload() {
            fetch(new Request("/ClearCookie", { method: "POST", keepalive: !0, mode: "same-origin", credentials: "same-origin" }));
}

Where the fetch response has a SetCookie header that clears the existing one.

I was able to reproduce this in the CefSharp.WinForms.Example and was hoping to find a workaround/fix to this that we could use. What's strange is that if you instead close the tab with the CTRL+W hotkey, the fetch responses are able to update the cookie cache!

With the CefSharp.WinForms.Example project and a razor page that demonstrates the cookie handling I can do the following workflow:

  1. Download the demo website and dotnet run it
  2. Open http://localhost:5022 in 2 browser tabs
  3. Click the "Set cookie by client" button in one tab, and refresh both tabs by hitting "Go" in each
  4. In one tab, select the "SERVER" handler radio button and click File > Close Tab
  5. In the second tab, either refresh or click "Re-check cookie" and see the cookie has not been updated in the browser (it should have been cleared by step 4, but wasn't)

If I repeat the above, but in step 4 use the CTRL+W hotkey instead of File > CloseTab to close the tab, the cookies do get cleared! In this case there is a noticeable lag before the tab control/page is completely closed (probably something that is waiting on the fetch call).

The cookies are still not cleared if, in step 4, I swap the unload event to the pagehide (when not persisted) event. This swaps what event we fire the fetch on, and for pagehide swaps to a handler function that respects the event.persisted value:

function serverHandlePageHide(event) {
            if (event.persisted) {
                /* the page isn't being discarded, so it can be reused later */
            } else {
                fetch(new Request("/ClearCookie", { method: "POST", keepalive: !0, mode: "same-origin", credentials: "same-origin" }));
            }
        }

I bring this up because I realize unload is flagged as deprecated, but its replacement of pagehide doesn't work either. For reference:

If you're specifically trying to detect page unload events, the pagehide event is the best option.

The cookies are cleared if, in step 4, I select the "CLIENT" handler instead of "SERVER". In this case, this swaps the handler to just set document.cookie instead of using a fetch response to do so. It seems that if the handlers can run immediately, then they have time to clear the cookies:

function clientHandlerUnload() {
            document.cookie = 'myCookie=; expires=Thu, 01 Jan 1970 00:00:00 GMT';
        }

Note this isn't a workaround in our case since we want the server to also invalidate the session it has for the cookie before we clear it from the browser.

Expected behavior

Closing a browser tab from WinForms should support the same termination handling as closing from the browser process (what I assume CTRL+W is doing that File > Close Tab is not in the example).

Actual behavior

Closing a browser tab from WinForms does not support the fetch > clear cookies workflow on termination.

Regression?

I've been having build issues (so many VS errors) and have only been able to test things in v124.3.80.

Known Workarounds

No response

Does this problem also occur in the CEF Sample Application

No

Other information

For cefclient I did not see the issue when I tried Tests > New Window (or Popup Window) and treat that like the tab to close.

@amaitland
Copy link
Member

Does cefsharp/CefSharp.MinimalExample#147 (comment) work in the context of the minimal example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants