2017-09-11 24 views
0

以下の方法は15分後にユーザーをログアウトします。ただし、ユーザーがアクティブであっても問題がログアウトされます。15分後の自動ログアウトc#

私はユーザーがでないときにアクティブを15分間完全に解決する方法を探していますが、方法が実行されない以外はログアウトします。

public void AutoRedirect() 
{ 
    int int_MilliSecondsTimeOut = (this.Session.Timeout * 900000); 
    string str_Script = @" 
    <script type='text/javascript'> 
    intervalset = window.setInterval('Redirect()'," + int_MilliSecondsTimeOut.ToString() + @"); 
    function Redirect() 
    { 
     alert('Your session has been expired and system redirects to login page now.!\n\n'); 
     window.location.href='../index.aspx'; 
    } 
    </script>"; 
    UtilityClass.RemoveCookie("login", Response); 
    ClientScript.RegisterClientScriptBlock(this.GetType(), "Redirect", str_Script); 
} 
+0

ページがあるたびに、ページが最後にナビゲートされた時刻を含むセッションvarをチェックします。 15分未満の場合は、そのセッション変数を現在の時刻に更新し、ユーザーに続行させます。それ以外の場合は、セッション変数をヌルにしてホームページにリダイレクトします。 –

答えて

1

は、代わりに15分を実行している1つのタイマーでこれを行うには、どのように1分間隔の設定について、イベントにカウンターをカウントダウンしますか?

このカウンタは他のイベント、たとえばAJAX呼び出し、イベントをクリックするなど、簡単にjQuery bv .delegateまたは.onメソッドでトリガーすることができます。

このようなリセットが発生しない場合、カウントダウンは0になり、リダイレクトできます。あなたが見ることができる

var inactivityCountdown = 15; 

intervalid = window.setInterval(function Redirect() 
{ 
    inactivityCountdown--; 
    if (inactivityCountdown<1) { 
    clearInterval(intervalid); 
    alert('Your session has been expired and system redirects to login page now.!\n\n'); 
    window.location.href='../index.aspx'; 
    } 
}, 60*1000); 

、この方法は、それは有効期限が切れる前にカウンタinactivityCountdownいつでもをリセットするのは簡単です。リダイレクトを行うときに、間隔をクリアすることを忘れないでください。

次のスニペットでは、より良いテストのために、分ではなく10分を使用しています。テキストフィールドに何かを入力するか、ボタンをクリックしてカウントダウンをリセットします。

あなたはあなたがリンクを経由して再度ログインすることができますメッセージ、その後ログアウトテキスト(これはリダイレクトをシミュレート)を取得:

var initCountdownValue = 10; 
 
var inactivityCountdown = initCountdownValue; 
 
$(document).ready(function() { 
 

 
    $(document).delegate("#login", "click", function(e) { 
 
    location.reload(); 
 
    }); 
 

 
    function Logout() { 
 
    document.getElementById('mainpage').style.display = "none"; 
 
    document.getElementById('loggedout').style.display = ""; 
 
    // or window.location.href="/logout.aspx"; 
 
    } 
 

 
    var intervalid = window.setInterval(function Redirect() { 
 
    inactivityCountdown--; 
 
    $("#counter").text(inactivityCountdown.toString()); 
 
    if (inactivityCountdown < 1) { 
 
     clearInterval(intervalid); 
 
     Logout(); 
 
     alert('Your session has been expired and system redirects to login page now.!'); 
 
    } 
 
    }, 1 * 1000); 
 

 
    $(document).delegate(":input", "click change keydown", function() { 
 
    inactivityCountdown = initCountdownValue; 
 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> 
 
</script> 
 
<div id="mainpage" style=""> 
 
    <input type="text"></input> 
 
    <input type="button" text="test"></input> 
 
    <div> 
 
    Counter: 
 
    <div id="counter"></div> 
 
    </div> 
 
</div> 
 
<div id="loggedout" style="display:none"> 
 
    You are logged out. To login, click <a id="login" href="#">here</a> 
 
</div>

+0

偉大なもう1つのこと私はあなたが意味するものを理解しているかどうかわからないが、aspラベルのおかげでカウンタ値を表示することができます – Ayman

+0

私はちょっとスニペットを変更しているので、画面上のカウントダウンを表示します。 – Matt

+0

助けてくれてありがとう – Ayman

0

だから、window.setIntervalはあなたがそう考えるかもしれないので、上書きされません。 この機能を削除して、IISまたは何かがそれを設定することでそれを処理させることができます。

またはここに言ったように、あなたは、単に間隔をクリアして、もう一度それを設定することができます。 How do I stop a window.setInterval in javascript?

関連する問題