Session Timeout Notification in asp.Net web apps

Aypn CNN 446 Reputation points
2024-02-11T04:33:33.21+00:00

Hi, in my aspx .net web apps (VB), (devt tool VS 2022), I want to enable session with showing popup message, it must restart the session when the user clicks on any element, this would be performed by jQuery script, ref below my web apps property / attributes/ requirements ;

  1. Most of the functions are I using and executing procedures in Java, jQuery with asmx service
  2. I can connect client side Jquery to Asmx to SQL SPs
  3. I may touch VB code-behind few areas only i.e., when form load, data bind to repeaters
  4. I want show session warning message in 3 mins like this (ref below link), when user click any element in page then it would restart URL: Sample, I tested
  5. Want to show below message , if user click any button in same page or different page (in this project only) then session should be restarted, otherwise it would redirect to MasterMenu.aspx (my dashboard page) User's image

I hope understand my requirement pls provide your script in jquery or suitable one.

Developer technologies ASP.NET Other
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2024-02-13T03:14:35.4233333+00:00

    Hi @Aypn CNN,

    From what I understand, your current requirement is that when the user clicks on the page, the session must be restarted.

    I've tested a simple example below. Assuming that the session time is 60 seconds, a session timeout notification will appear when it reaches 50 seconds.

    When you click on the page, the session will refresh.

    Demo

    The Session Timeout has been set to one minute in Web.Config.

     <system.web>
    // ***
      <sessionState timeout = "1"/>
     </system.web>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
        <link href="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/start/jquery-ui.css"
            rel="stylesheet" type="text/css" />
        <script type="text/javascript">
            $('html').on('click', function () {
                ResetSession();
            })
            $(function () {
                $("#dialog").dialog({
                    autoOpen: false,
                    modal: true,
                    title: "Session Expiring",
                    buttons: {
                        Ok: function () {
                            ResetSession();
                        },
                        Close: function () {
                            $(this).dialog('close');
                        }
                    }
                });
            });
            function SessionExpireAlert(timeout) {
                var seconds = timeout / 1000;
                $("#secondsIdle").html(seconds);
                $("#seconds").html(seconds);
                setInterval(function () {
                    seconds--;
                    $("#secondsIdle").html(seconds);
                    $("#seconds").html(seconds);
                }, 1000);
                setTimeout(function () {
                    //Show Popup before 50 seconds of timeout.
                    $('#dialog').dialog('open');
                }, timeout - 50 * 1000);
                setTimeout(function () {
                    window.location = "MasterMenu.aspx";
                }, timeout);
            };
            function ResetSession() {
                //Redirect to refresh Session.
                window.location = window.location.href;
            };
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <h3>Session Idle:&nbsp;<span id="secondsIdle"></span>&nbsp;seconds.</h3>
            <div id="dialog">
                Your Session will expire in&nbsp;<span id="seconds"></span>&nbsp;seconds.<br />
                Do you want to reset?
            </div>
        </form>
    </body>
    </html>
    
     Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
         Response.Cache.SetCacheability(HttpCacheability.NoCache)
         If Not Me.IsPostBack Then
             Session("Reset") = True
             Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration("~/Web.Config")
             Dim section As SessionStateSection = DirectCast(config.GetSection("system.web/sessionState"), SessionStateSection)
             Dim timeout As Integer = CInt(section.Timeout.TotalMinutes) * 1000 * 60
             ClientScript.RegisterStartupScript(Me.GetType(), "SessionAlert", "SessionExpireAlert(" & timeout & ");", True)
         End If
     End Sub
    

    User's image

    Best regards,
    Lan Huang


    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.