我使用基本身份驗證爲我的網站,並有一個問題,當用戶需要一段時間來填寫表格,他們的會話可能結束,並在單擊「保存」的工作將消失,因爲他們的會話不再有效。警告和擴展用戶會話
我試過如下:
設置會話超時1分鐘在web.config中:
<system.web> <sessionState timeout="1"></sessionState>
添加下面的JavaScript到我的網頁,這是爲了延長每30秒一次會話:
AlertTimeOut = 30000; // Milliseconds, Alert Message
WarnTimeOut = 30000; // Warn Message before 30 sec of session out
setTimeout("WarnUser();", WarnTimeOut);
//setTimeout("AlertUser();", AlertTimeOut);
function AlertUser() {
alert('Your session has been expired, please save your data outside the system [copy the data in word document].');
}
function WarnUser() {
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10) {
minutes = "0" + minutes
}
if (hours > 11) {
var tempTime = hours + ":" + minutes + "PM"
} else {
var tempTime = hours + ":" + minutes + "AM"
}
var ans = confirm("Your login session is about to expire in 5 minutes.Do you want to extend it? - Date: " + tempTime);
if (ans) {
var oImage = new Image;
oImage.src = "tempdummy.gif" + Math.random();
setTimeout("WarnUser();", WarnTimeOut);
}
else {
setTimeout("AlertUser();", AlertTimeOut);
}
}
這是我的main.aspx.cs頁面的代碼片段即,等待JavaScript之後彈出來了2次,我刷新頁面,是的,session["userid"]
是null
。我調試了JavaScript,它確實打擊了每一行代碼。
protected void Page_Load(object sender, EventArgs e)
{
if (Session["userid"] == null)
{
// the session is no longer valid, i guess the javascript didn't work :(
//redirect user to an error page
}
else
{
username = (string)Session["userid"];
// check user permission, and populate the form here
}
}
我發現安靜的使用類似的代碼的幾個例子在線:
var oImage = new Image;
oImage.src = "tempdummy.gif" + Math.random();
延長會議,但我想我只是不理解它是如何工作的。這是一個圖像,我應該有一個地方在我的aspx頁面?它是如何工作的?因爲它不適合我
---- || ---- || ---- || ---- || ---- || ---- || ---- ||
更新:
我也試過如下:
創建refresh_session.aspx頁面只有1這行代碼:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("success");
}
然後我嘗試發佈到該頁面刷新垃圾(按照這篇文章:http://naspinski.net/post/Automatically-refresh-your-users-Session-behind-the-scenes-using-jQuery-and-AspNet.aspx)
我更新的JavaScript看起來像:
AlertTimeOut = 30000; // Milliseconds, Alert Message
WarnTimeOut = 30000; // Warn Message before 30 sec of session out
setTimeout("WarnUser();", WarnTimeOut);
function TimedOut() {
$.post("refresh_session.aspx", null,
function (data) {
if (data == "success") {
setTimeout("TimedOut();", WarnTimeOut);
alert('refreshed');
}
else { alert('not refreshed'); }
}
);
}
function AlertUser() {
alert('Your session has been expired, please save your data outside the system [copy the data in word document].');
}
function WarnUser() {
var ans = confirm("Your login session is about to expire in 5 minutes.Do you want to extend it? ");
if (ans) {
var oImage = new Image;
oImage.src = "tempdummy.gif";
TimedOut() ;
setTimeout("WarnUser();", WarnTimeOut);
}
else {
setTimeout("AlertUser();", AlertTimeOut);
}
}
在frefresh_session斷點被擊中,但後來當我刷新我的網頁它仍然在我的Default.aspx NULL:((((((
你是否收到任何javascript錯誤?嘗試在Firebug中添加一個用於firefox的斷點(或者在Chrome/IE中使用開發工具 - >在瀏覽器中點擊F12)。嘗試查明是否發出了通話後,並且如果響應確實是'成功' –
它已經在幾次Ctrl-F5刷新後奇蹟般地修復了它,它現在確實打到了refresh_session.aspx頁面,但是會話仍然爲空我再次刷新頁面以測試它在我的default.aspx :(即時通訊如此困惑 –