我尝试通过在web.config中设置authentication / forms / timeout值,并在登录页面FormsAuthenticationTicket.expiration中设置代码,在我的网站上设置相当长的60分钟会话时间。但是,已经在20分钟内,太早了,我的Session变量中的数据丢失了。这是如何工作的,我该如何解决?
的Web.config:
<authentication mode="Forms">
<forms name="MYWEBAPP.ASPXAUTH"
loginUrl="~/Login.aspx"
protection="All"
timeout="60" /* <<------ */
slidingExpiration="true"
path="/"/>
</authentication>
login.aspx.cs:
// Create a new ticket used for authentication
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // Ticket version
LoggedInUser.LoginNm.ToString(), // Username associated with ticket
DateTime.Now, // Date/time issued
DateTime.Now.AddMinutes(60), // <<--- // Date/time to expire, minutes, use same value as in web.config or result is unclear
false, // "true" for a persistent user cookie
LoggedInUser.UserID.ToString(), // User-data, in this case user object
FormsAuthentication.FormsCookiePath); // Path cookie valid for
// Encrypt the ticket and store in a cookie
string CookieName = FormsAuthentication.FormsCookieName;
string CookieValue = FormsAuthentication.Encrypt(ticket);
cookie = new HttpCookie(CookieName, CookieValue);
// Set the cookie's expiration time to the tickets expiration time, plus a little to be sure.
// NB: the cookie is created and updated with "sliding" reset automatically
// if the SetAuth...() coockie call was used
// NB: Persistent is false, so this is a Session cookie without expiration
// that is deleted when the browser closes.
//if (ticket.IsPersistent)
{
cookie.Expires = ticket.Expiration.AddMinutes(5);
}
在form.aspx.cs中检查会话是否仍然存在:
protected void Page_PreInit(object sender, EventArgs e)
{
// Load settings from session variabele
MySession = Session["MySession"] as ClSession;
// return to loginpage indien sessionvar = null
if (MySession == null)
Response.Redirect("~/SessionExpired.html"); // <<---after only 20 minutes!!??
return;
}