cefsharp / cefsharp/CefSharp

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

Aperta
#4,846 10 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
C#
Stelle
10.3k
Fork
2.9k
Merge medio
5g 22h
PR unite (30g)
4

Descrizione

### Is there an existing issue for this?

- [X] 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](https://github.com/madaster97/term-fetch-cookie) 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:
```js
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](https://developer.mozilla.org/en-US/docs/Web/API/Window/pagehide_event):
> 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:
```js
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.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia nel progetto CefSharp.WinForms.Example confrontando il comando File > Close Tab con il percorso di chiusura della scheda tramite CTRL+W, quindi riproduci il comportamento usando la demo term-fetch-cookie fornita. Traccia la gestione della terminazione della pagina e il comportamento di aggiornamento dei cookie; il lavoro è completato quando l’azione del menu WinForms consente alla risposta keepalive fetch di cancellare il cookie, come fa il percorso del tasto di scelta rapida.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp, javascript
Ambito
desktop
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.