Aşağıda ASP.NET Kimlik zaman aşımı Forms ve zaman aşımı ulaşılırsa giriş sayfasına onları yönlendirir konusunda uyarmak için jQuery ile bazı JavaScript olduğunu. Oturum zaman aşımı için de geliştirilebilir ve uyarlanabilir. Ayrıca, kullanıcı sayfaya tıklayarak, yazarak veya yeniden boyutlandırarak etkileşimde bulunduğunda sunucunun "pinglenmesi" suretiyle kimlik doğrulama zaman aşımını da sıfırlayacaktır.
Bunun, her tıklamayla ping yaparak, tuşa basıp, yeniden boyutlandırarak sunucuya yük eklediğini, ancak yeniden boyutlandırmanın oldukça az olduğunu unutmayın. Yine de, yazarken çok sayıda kullanıcınız varsa, bu etkiyi değerlendirmeniz gerekecek. Bunu yapmanın başka bir yolu düşünemedim çünkü sunucu zaman aşımının süresinin dolduğundan beri dahil olmalı.
Ayrıca zaman aşımının JS'de kodlanmış olmadığını unutmayın. Sunucudan zaman aşımı oluyor, böylece Web.config'te tek bir yerde tutmanız gerekiyor.sunucuda
window.session.monitorAuthenticationTimeout(
"/Account/Login", // You could also use "@FormsAuthentication.LoginUrl" in Razor.
"/Account/Ping");
, kalan zamanını döndüren bir eylem gerekecek: Sayfa yüklendiğinde zaman
(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);
Basitçe bir kez yukarıdaki diyoruz. Daha fazla bilgi de ekleyebilirsiniz.
public JsonResult Ping()
{
return Json(new {
timeout = FormsAuthentication.Timeout.TotalMilliseconds
},
JsonRequestBehavior.AllowGet);
}
i bu bir baktığımız evet, ben sadece thx – cdub
var merak Ben ' sessionState> '. "Var sessionTimeoutWarnin" öğesini nasıl atayabilirim? –
cdub
, ya bu aslında oldukça iyi çalışıyor "doğru yol" – SearchForKnowledge