You must Sign In to post a response.
  • Category: ASP.Net MVC

    How to show a popup warning before session timeout in asp.net mvc 3


    Are you looking for the best way to check whether session is going to be expired? Want to display a message on screen before session get expired? Read this thread to get different ideas.



    I want to get a popup before session is going to be expired in ASP.NET MVC 3.
    So that user can continue/extend the session or he may logout also. This should be implemented in asp.net mvc 3.

    Thanks,
    Ram
  • #717380
    Here is the Exact Sample for your requirement

    http://community.devexpress.com/blogs/aspnet/archive/2011/07/11/asp-net-mvc-how-to-show-a-popup-warning-before-session-timeout-aspnetmvc.aspx

    Thanks & Regards,
    Danasegarane Arunachalam

    http://www.techreceipe.tk/

  • #717381
    Another Example : http://stackoverflow.com/questions/1740230/how-do-i-warn-the-user-that-their-web-session-is-about-to-time-out
    Thanks & Regards,
    Danasegarane Arunachalam

    http://www.techreceipe.tk/

  • #717481
    Hi,
    In LayOut Page u need to add following script.

    <script type="text/javascript">
    //code for expired session
    var sTimer = null;
    var eTimer = null;

    function InitializeTimer() {

    //30 minutes
    var interval = 1800000; //time in milliseconds 1000 ms = 1 second

    settimerFun(interval);

    $('body').ajaxStart(function () {
    // reset timer on ajax request
    settimerFun(interval);
    });
    }
    function settimerFun(interval) {
    if (sTimer)
    clearTimeout(sTimer);

    sTimer = setTimeout(function () { sExpired(); }, interval);
    }
    function sExpired() {

    //for yes or no dialog
    $('#div_sexpired').remove();
    var dialog = $('<div id="div_sexpired"></div>').text("Do you want to extend the session?");
    $('body').append(dialog);

    $('#div_sexpired').dialog({
    autoOpen: true,
    modal: true,
    title: 'Extend Session',
    buttons: {
    "Yes": function () {
    //write your logic here for yes button
    if (eTimer)
    clearTimeout(eTimer);
    settimerFun(5000);
    $(dialog).dialog('close');
    $('#div_sexpired').remove();
    },
    "No": function () {
    //write your logic here for no button
    redirtoLogin
    }
    }

    });



    //solution for your problem
    eTimer = setTimeout(function () {
    $(dialog).dialog('close'); //close the yer/no dialog

    //dialog for expired session
    var expiredialog = $('<div id="div_expired"></div>').text("Your session has timed out. Please login again.");
    $('body').append(expiredialog);
    $('#div_expired').dialog({
    autoOpen: true,
    modal: true,
    title: 'Expired Session',
    buttons: {
    "OK": function () {

    clearTimeout(eTimer);
    $(this).dialog('close');
    }

    },
    "close": redirtoLogin
    //on the close of this dialog redirect to login page
    });


    }, 1800000); //invoked after three seconds


    }

    function redirtoLogin() {
    //redirect to login page
    window.location.href = '@Url.Action("Login", "Home")';
    }

    //end of expired session

    $(function () {

    InitializeTimer(); //invoke the timer function
    });

    </script>


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.