当前,我正在为Web应用程序编程,但是某些同事有时会提到,他们在使用Web应用程序时会遇到会话超时。 我无法重现错误,也不确定可能是什么错误。因为进行中的工作有足够的页面加载,所以自上次单击以来的时间比15分钟的超时短。还有什么可能是原因?
Web.config:
<sessionState mode="InProc" timeout="3" customProvider="DefaultSessionProvider">
自动重定向C#:
public partial class AutoRedirect : System.Web.UI.UserControl
{
public string LoginDate;
public string ExpressDate;
protected void Page_Load(object sender, EventArgs e)
{
string PageName = Path.GetFileName(Request.Url.AbsolutePath);
// Check session is expire or timeout.
if (Session["username"] == null)
{
if (PageName != Constants.defaultPage && PageName != Constants.loginPage)
{
Response.Redirect(Constants.defaultPage);
}
}
// Get user login time or last activity time.
DateTime date = DateTime.Now;
LoginDate = date.ToString("u", DateTimeFormatInfo.InvariantInfo).Replace("Z", "");
int sessionTimeout = Session.Timeout;
DateTime dateExpress = date.AddMinutes(sessionTimeout);
ExpressDate = dateExpress.ToString("u", DateTimeFormatInfo.InvariantInfo).Replace("Z", "");
}
}
自动重定向ascx:
<script type="text/javascript">
var timeRefresh;
var timeInterval;
var currentTime;
var expressTime;
expressTime = "<%=ExpressDate %>";
currentTime = "<%=LoginDate %>";
setCookie("express", expressTime);
timeRefresh = setInterval("Refresh()", 1000);
// Refresh this page to check session is expire or timeout.
function Refresh() {
var current = getCookie("express");
var date = current.split(" ")[0];
var time = current.split(" ")[1];
var scriptDate = new Date();
var year = scriptDate.getFullYear();
var month = scriptDate.getMonth() + 1;
var day = scriptDate.getDate();
var hour = scriptDate.getHours();
var min = scriptDate.getMinutes();
var second = scriptDate.getSeconds();
if (Date.UTC(year, month, day, hour, min, second) >=
Date.UTC(date.split("-")[0], date.split("-")[1], date.split("-")[2],
time.split(":")[0], time.split(":")[1], time.split(":")[2])) {
clearInterval(timeRefresh);
Redirect();
}
}
function Redirect() {
window.location.replace("Login.aspx?s=ex");
}
// Retrieve cookie by name.
function getCookie(name) {
var arg = name + "=";
var aLen = arg.length;
var cLen = document.cookie.length;
var i = 0;
while (i < cLen) {
var j = i + aLen;
if (document.cookie.substring(i, j) == arg) {
return getCookieVal(j);
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return;
}
function getCookieVal(offSet) {
var endStr = document.cookie.indexOf(";", offSet);
if (endStr == -1) {
endStr = document.cookie.length;
}
return unescape(document.cookie.substring(offSet, endStr));
}
// Assign values to cookie variable.
function setCookie(name, value) {
document.cookie = name + "=" + escape(value);
}
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
$(document).ready(function () {
if (getParameterByName('s') == 'ex') {
OpenDialogForSessionExpired();
}
})
</script>
答案 0 :(得分:1)
请在global.asax中检查Session_Start。
您设置了PSQL Exception
吗?
还可以使用重定向:
Session.Timeout = x
在您的首页Page_Load方法中。