Skip to content

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

Open
@madaster97

Description

@madaster97

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions