Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

.net - Session End in ASP.net MVC

I am placing user data in the session then passing it to viewdata to view on a page. Now when my user walks away from his computer for a while, the session ends and the data he entered is lost.(that is ok) The problem is that when he returns he refreshes the screen my page is still showing but all the data is gone. So,

How can I redirect the user to a login screen when the session expires (so he doesnt see a blank page) or I would like to redirect him to a page that states "You have been logged out due to inactivity".

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I use some Javascript in my MasterPage to redirect the user (after a prompt to renew the session) to the logout action. It uses an AJAX request back to the Home page of the app to refresh the server side session window when the user clicks the button in the dialog to extend the session. Relies on jQuery and jQuery UI for the dialog.

 <% if (this.Request.IsAuthenticated)
    {
        int sessionDialogWait = 2 * 60 * 1000 - 60 * 500; // ms = 1.5 minutes
        int sessionTimeout = 28 * 60 * 1000; // ms = 28 minutes
        if (ViewData["sessionTimeout"] != null)
        {
            sessionTimeout = ((int)ViewData["sessionTimeout"] * 60 - 120) * 1000;
        }
%>  
<script type="text/javascript">
    var logoutTimer = null;
    var sessionTimer = null;
    var sessionTimeout = Number('<%= sessionTimeout %>');
    var sessionDialogWait = Number('<%= sessionDialogWait %>');

    $(document).ready( function() {
        $('#sessionEndDialog').dialog( {
            autoOpen: false,
            bgiframe: true,
            modal: true,
            buttons: {
                OK: function() {
                    $(this).dialog('close');
                    $.get( '<%= Url.Action( "About", "Home" ) %>', scheduleSessionPrompt, 'html' );
                },
                Logout: logoutOnSessionExpires
            }
        }).ajaxStart( function() { scheduleSessionPrompt(); } );
        scheduleSessionPrompt();
    });

    function scheduleSessionPrompt()
    {
        if (logoutTimer) clearTimeout(logoutTimer);
        if (sessionTimer) clearTimeout(sessionTimer);

        sessionTimer = setTimeout( sessionExpiring, sessionTimeout  );
    }

    function sessionExpiring()
    {
         logoutTimer = setTimeout( logoutOnSessionExpires, sessionDialogWait );
         $('#sessionEndDialog').dialog('open');
    }

    function logoutOnSessionExpires()
    {
        window.location.href = '<%= Url.Action( "Logout", "Account" ) %>';
    }       

    </script>
<% } %>

<div id="sessionEndDialog" title="Session Expiring" style="display: none;">
    <p>Your session is about to expire.  Click OK to renew your session or Logout to logout of the application.</p>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...