I work on blazor server side .I face issue session state not redirect to login after 2 minutes .
so I need when user leave his page after 2 minutes then redirect to login page
I applied code but my code not working why this code not working I don’t know why not work .
what I try
1- I create JS file include redirect login after 2 minutes
function checkSessionTimeout(currentUrl) {
var sessionTimeout = 2 * 60 * 1000; // 2 minutes in milliseconds
var lastActivity = new Date(Date.parse(sessionStorage.getItem("LastActivity"))); // get the last activity time from the client-side session
if (new Date() - lastActivity > sessionTimeout) {
sessionStorage.clear(); // clear the session storage
window.location.href = '/login?returnUrl=' + encodeURIComponent(currentUrl); // redirect to login page with return URL
}
else {
setTimeout(function () { checkSessionTimeout(currentUrl); }, 1000); // check again in 1 second
}
}
checkSessionTimeout(window.location.href);
2-on _host file I added JS file on body section
<script src="_framework/blazor.server.js"></script>
<script src="~/assets/js/checksessiontimeout.js"></script>
3-on load of blazor page I set session
protected override void OnInitialized()
{
JS.InvokeVoidAsync("sessionStorage.setItem", "LastActivity", DateTime.Now.Ticks.ToString());
//Session.SetInt32("LastActivity", (int)DateTime.Now.Ticks * -1);
}
4-after component load i run this code below :
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JS.InvokeVoidAsync("checkSessionTimeout", navigationManager.Uri);
}
}
so my issue why page not redirect to login after 2 minutes ?