2013-11-01 6 views
0

У меня есть тестовая страница Mock (без проверки подлинности) и сохранение данных выбора пользователя в переменных сеанса, а на странице результатов - получение данных из этих сеансов. Время ожидания сеанса устанавливается равным 20 минутам с допустимым сроком действия. Время от времени пользователь простаивает более 20 минут (тайм-аут по умолчанию). Я хочу показывать предупреждение только тогда, когда сеанс завершен и перенаправлен. Я открыт для использования javascript, jquery или asp.net C#. Я видел похожие вопросы. & ответы на вопросы Tried Global.asax session и jquery, но никто не работает для меня. Может ли какой-нибудь орган дать мне пример в соответствии с моими требованиями? благодаряКак оповестить анонимного пользователя о тайм-ауте сеанса и перенаправить

ответ

1

Это отлично работает для меня

override protected void OnInit(EventArgs e) 
{ 
    base.OnInit(e); 

    //It appears from testing that the Request and Response both share the 
    // same cookie collection. If I set a cookie myself in the Reponse, it is 
    // also immediately visible to the Request collection. This just means that 
    // since the ASP.Net_SessionID is set in the Session HTTPModule (which 
    // has already run), thatwe can't use our own code to see if the cookie was 
    // actually sent by the agent with the request using the collection. Check if 
    // the given page supports session or not (this tested as reliable indicator 
    // if EnableSessionState is true), should not care about a page that does 
    // not need session 
    if (Context.Session != null) 
    { 
     //Tested and the IsNewSession is more advanced then simply checking if 
     // a cookie is present, it does take into account a session timeout, because 
     // I tested a timeout and it did show as a new session 
     if (Session.IsNewSession) 
     { 
      // If it says it is a new session, but an existing cookie exists, then it must 
      // have timed out (can't use the cookie collection because even on first 
      // request it already contains the cookie (request and response 
      // seem to share the collection) 
      string szCookieHeader = Request.Headers["Cookie"]; 
      if ((null != szCookieHeader) && (szCookieHeader.IndexOf("ASP.NET_SessionId") >= 0)) 
       ScriptManager.RegisterStartupScript(this, this.GetType(), "Redirect", "alert('Your session has timed out due to inactivity. Please start the test again'); window.location.reload()", true); 
     } 
    } 
} 
0
idleTime = 0; 
$(document).ready(function() { 

    var idleInterval = setInterval(timerIncrement, 5000); 


    $(this).mousemove(function (e) { 
     idleTime = 0; 
    }); 
    $(this).keypress(function (e) { 
     idleTime = 0; 
    }); 
}); 

function timerIncrement() { 
    idleTime = idleTime + 1; 
    if (idleTime >12) { //60 secs 
     alert("session expired"); 
     window.location.href="a3.html"; 
    } 
} 

это кодирование помогает таймауту сеанса после 60secs и перенаправить на другую страницу после оповещения

Смежные вопросы