以下は、ASP.NETフォーム認証のタイムアウトについてユーザーに警告するjQueryのJavaScriptで、タイムアウトに達するとログインページにリダイレクトされます。これは、セッションタイムアウトに対しても改善され適応される可能性があります。また、ユーザーがクリック、入力、またはサイズ変更してページとやりとりするたびに、サーバーに「ping」することで認証のタイムアウトをリセットします。
これは、クリック、キー押下、サイズ変更のたびにpingを実行することでサーバーに負荷がかかることに注意してください。それでも、多くのユーザーが入力した場合、その影響を評価する必要があります。私はこれを行うための別の方法は考えられませんでした。なぜなら、タイムアウトが過ぎてからサーバーが関与しなければならないからです。
また、タイムアウトはJSでハードコードされていないことに注意してください。それはWeb.configの1つの場所でそれを維持する必要があるので、サーバーからのタイムアウトを取得します。サーバー上で
window.session.monitorAuthenticationTimeout(
"/Account/Login", // You could also use "@FormsAuthentication.LoginUrl" in Razor.
"/Account/Ping");
、あなたは残り時間を返すアクションをする必要があります:あなたのページの負荷がとき
(function ($, undefined) {
if (!window.session) {
window.session = {
monitorAuthenticationTimeout: function (redirectUrl, pingUrl, warningDuration, cushion) {
// If params not specified, use defaults.
redirectUrl = redirectUrl || "~/Account/Login";
pingUrl = pingUrl || "~/Account/Ping";
warningDuration = warningDuration || 45000;
cushion = cushion || 4000;
var timeoutStartTime,
timeout,
timer,
popup,
countdown,
pinging;
var updateCountDown = function() {
var secondsRemaining = Math.floor((timeout - ((new Date()).getTime() - timeoutStartTime))/1000),
min = Math.floor(secondsRemaining/60),
sec = secondsRemaining % 60;
countdown.text((min > 0 ? min + ":" : "") + (sec < 10 ? "0" + sec : sec));
// If timeout hasn't expired, continue countdown.
if (secondsRemaining > 0) {
timer = window.setTimeout(updateCountDown, 1000);
}
// Else redirect to login.
else {
window.location = redirectUrl;
}
};
var showWarning = function() {
if (!popup) {
popup = $(
"<div style=\"text-align:center; padding:2em; color: black; font-color: black; background-color:white; border:2px solid red; position:absolute; left: 50%; top:50%; width:300px; height:120px; margin-left:-150px; margin-top:-90px\">" +
"<span style=\"font-size:1.4em; font-weight:bold;\">INACTIVITY ALERT!</span><br/><br/>" +
"You will be automatically logged off.<br/><br/>" +
"<span style=\"font-size:1.4em; font-weight:bold;\" id=\"countDown\"></span><br/><br/>" +
"Click anywhere on the page to continue working." +
"</div>")
.appendTo($("body"));
countdown = popup.find("#countDown");
}
popup.show();
updateCountDown();
};
var resetTimeout = function() {
// Reset timeout by "pinging" server.
if (!pinging) {
pinging = true;
var pingTime = (new Date()).getTime();
$.ajax({
type: "GET",
dataType: "json",
url: pingUrl,
}).success(function (result) {
// Stop countdown.
window.clearTimeout(timer);
if (popup) {
popup.hide();
}
// Subract time it took to do the ping from
// the returned timeout and a little bit of
// cushion so that client will be logged out
// just before timeout has expired.
timeoutStartTime = (new Date()).getTime();
timeout = result.timeout - (timeoutStartTime - pingTime) - cushion;
// Start warning timer.
timer = window.setTimeout(showWarning, timeout - warningDuration);
pinging = false;
});
}
};
// If user interacts with browser, reset timeout.
$(document).on("mousedown mouseup keydown keyup", "", resetTimeout);
$(window).resize(resetTimeout);
// Start fresh by reseting timeout.
resetTimeout();
},
};
}
})(jQuery);
は、単に一度上記を呼び出します。さらに情報を追加することもできます。
public JsonResult Ping()
{
return Json(new {
timeout = FormsAuthentication.Timeout.TotalMilliseconds
},
JsonRequestBehavior.AllowGet);
}
私はこの1つを見てきたええ、私はちょうどTHX – cdub
だ思っていた私は、 '<てsessionStateモード=「は、StateServer」Cookieなし=「false」を追加私のWeb.configファイルでtimeout = "180"> sessionState> ' 'var sessionTimeoutWarnin'をどうやって割り当てるのですか? – cdub
、何これは実際には非常にうまく機能し、「適切な方法」 – SearchForKnowledge