admin管理员组

文章数量:1406968

I'm developing a Blazor Server application with ASP.NET Core Identity and cookie-based authentication. I have implemented sliding expiration for the authentication cookie.

The challenge is to force an immediate redirection to an "inactive session" page when the session is invalidated. Inactivity, network issues, don't care.

If the server begins to clean up the session for any reason then attempt to redirect.

This is crucial to avoid situations where the user continues interacting with the app after their session has expired, only to experience errors or lost data when trying to submit forms or make server requests. this is usually related to inactivity but other situations may apply. Currently the app just locks up.

Redirecting on the next client server interaction is way to late. You can interact a lot with a Blazor app without having to make a round trip.

Specifically, I need the server to detect when the session has been invalidated (for any reason, not just inactivity) and automatically redirect the user to the /inactive-session page before any interaction with the app takes place.

I'm developing a Blazor Server application with ASP.NET Core Identity and cookie-based authentication. I have implemented sliding expiration for the authentication cookie.

The challenge is to force an immediate redirection to an "inactive session" page when the session is invalidated. Inactivity, network issues, don't care.

If the server begins to clean up the session for any reason then attempt to redirect.

This is crucial to avoid situations where the user continues interacting with the app after their session has expired, only to experience errors or lost data when trying to submit forms or make server requests. this is usually related to inactivity but other situations may apply. Currently the app just locks up.

Redirecting on the next client server interaction is way to late. You can interact a lot with a Blazor app without having to make a round trip.

Specifically, I need the server to detect when the session has been invalidated (for any reason, not just inactivity) and automatically redirect the user to the /inactive-session page before any interaction with the app takes place.

Share Improve this question edited Mar 7 at 6:57 Qiang Fu 9,4071 gold badge6 silver badges16 bronze badges asked Mar 6 at 17:58 MikeNMikeN 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

To detect inactivity/idle you could follow this document https://learn.microsoft/en-us/aspnet/core/blazor/fundamentals/signalr?view=aspnetcore-9.0#monitor-server-side-circuit-activity.
Simply inject navigationmanager to perform redirect when idle.

        private void CircuitIdle(object? sender, System.Timers.ElapsedEventArgs e)
        {
            logger.LogInformation("{CircuitId} is idle", currentCircuit?.Id);
            _navigationManager.NavigateTo("/inactive-session");           
        }

You could set proper IdleTimeout instead of waiting for the websocket really disconnect (Server will not be able to perform client redirect because of the disconnect).

本文标签: aspnet coreDetecting session invalidationexpiration in NET 9 BlazorStack Overflow