cefsharp / cefsharp/CefSharp

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

Abierto
#4,846 10 comentarios 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
C#
Estrellas
10.3k
Forks
2.9k
Merge medio
5 d 22 h
PR fusionados (30 d)
4

Descripción

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

Guía de contribución

Abrir la guía de contribución

Línea de trabajo

Comienza en el proyecto CefSharp.WinForms.Example comparando el comando File > Close Tab con la ruta de cierre de pestañas mediante CTRL+W y, a continuación, reproduce el comportamiento usando la demo term-fetch-cookie proporcionada. Sigue el manejo de la terminación de la página y el comportamiento de actualización de cookies; se considera terminado cuando la acción del menú de WinForms permite que la respuesta de keepalive fetch borre la cookie, como lo hace la ruta del atajo de teclado.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
csharp, javascript
Área
desktop
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.