why session state not fire after 2 minute?

Ahmed Salah Abed Elaziz 390 Reputation points

I work on blazor server side . I face issue session state not redirect to login after 2 minutes . so I need when user leave current 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 checksessiontimeout 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


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 ?

A set of technologies in the .NET Framework for building web applications and XML web services.
4,249 questions
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,428 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,046 Reputation points Microsoft Vendor

    Hi @Ahmed Salah Abed Elaziz
    As AgaveJoe said, the issue relates the DateTime.Now.Ticks, when using JavaScript Date.parse method to convert the session value to the date time, it will show the "Invalid Date" error.
    To solve this issue, you can try to remove the Tricks property, like this:

    JS.InvokeVoidAsync("sessionStorage.setItem", "LastActivity", DateTime.Now.ToString());

    Then, the result as below: after time out, it will redirect to login page:
    [Note] when the page active, you need to reset the session storage.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Best regards,

    0 comments No comments