菜鸟问问:一个form身份验证票的问题(在线急等)
问题是:为什么我每次关掉浏览器后在访问index.aspx时还要重新登陆(每次打开都是过期的,是不是我的思路不对)
index.aspx.cs中该如何调用,判断?帮帮我各位!先谢谢!
login.aspx.cs页面:
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, Session["username"].ToString(), DateTime.Now,
DateTime.Now.AddMinutes(30),
false, Session["username"].ToString());
string cookieStr = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieStr);
cookie.HttpOnly = true;
cookie.Path = FormsAuthentication.FormsCookiePath;
//cookie.Expires = ticket.Expiration;
Context.Response.Cookies.Add(cookie);
FormsAuthentication.RedirectFromLoginPage(Session["username"].ToString(), false);
web.Config:
<authentication mode="Forms">
<forms name="ASPXFORMSAUTH" loginUrl="login.aspx" timeout="20" />
</authentication>
<authorization>
<deny users="?"/>
</authorization>
<sessionState mode="InProc" timeout="20" cookieless="UseCookies" />//=true
index.aspx.cs:
if (FormsAuthentication.FormsCookieName.ToString() == null)
{
Response.Redirect("login.aspx");
}
else
{
FormsAuthenticationTicket Ticket = FormsAuthentication.Decrypt(Context.Request.Cookies[FormsAuthentication.FormsCookieName].Value);
Response.Write(Ticket.Expiration.ToString());//这里输出的时间是在当前时间上加20分钟!
Session["username"] = Ticket.Name;
}