How to logout a user on Browser close in Blazor Server

Michael Ofori-Appiah 0 Reputation points
2024-01-28T17:05:34.7+00:00

I have a Blazor server application with Authentication and Authorization. I want to know how to logout a user when the user closes the browser tab without clicking the Logout button.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,323 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 52,826 Reputation points
    2024-01-28T17:52:37.6533333+00:00

    You logout a user by deleting the authentication cookie. The server can not do this if the tab is closed, as this closes the connection to the client. You can detect the signal/r circuit closed.

    https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.server.circuits.circuithandler?view=aspnetcore-8.0

    then store in a persistent store that the user failed to logout. Then in the next request from that user, force a logout by deleting the cookie.

    0 comments No comments