如下代码:
protected void ButtonSubmit_Click(object sender, EventArgs e)
{
int userid = CheckUser(TextBoxUserName.Text, TextBoxPassword.Text);
if (userid > 0)
{
username = TextBoxUserName.Text;
HttpCookie cookie = FormsAuthentication.GetAuthCookie(userid.ToString(), true);
cookie.Expires = DateTime.Now+ new TimeSpan(30,0,0,0);
Response.AppendCookie(cookie);
Response.Redirect(FormsAuthentication.GetRedirectUrl(userid.ToString(), true));
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (Request.IsAuthenticated)
{
hyperLinkEdit.NavigateUrl = "~/editlinks.aspx?userid= " + User.Identity.Name;
ButtonSubmit.Text = "注销 ";
}
else
{
ButtonSubmit.Text = "登陆 ";
}
}
刚登录时,把ie彻底关闭了再打开页面还是在登录状态的。
后来我为了测试cookie过期时间是否是30天就把windows时钟往后调了一个小时。
然后就不是登录状态了...
请大家帮忙指点一下了!
------解决方案--------------------------------------------------------
cookie.Expires = DateTime.Now+ new TimeSpan(30,0,0,0);
这个失效时间就是30天
你不设置就是浏览器关闭时,就直接失效
------解决方案--------------------------------------------------------
确实有点奇怪
按理说设置30天,就调后一小时不会过期的啊
可能。。。
------解决方案--------------------------------------------------------
按理说设置30天,就调后一小时不会过期的啊
============
那 30 天是往前算的,
即使你调后了 1 分钟,也是”过期“了,不是吗?
事实上应该说,在你调后的那个时刻,这件事还没发生呢,
------解决方案--------------------------------------------------------
会是缓存吗?
这个问题比较怪
------解决方案--------------------------------------------------------
给你看个保存cookie验证的代码:
// 设置cookie过期时间
DateTime dtExpricae = DateTime.Now.AddYears(1);
//发放Cookile验证
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
user.UserName, // 当前登入名
DateTime.Now, // cookie创建时间
dtExpricae, // Cookie过期时间
true, // 是否永久保存cookie
user.UserType.ToString()); // 附加用户角色信息
// 加密cookie信息
string hashTicket = FormsAuthentication.Encrypt(ticket);
// 手动添加cookie
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,hashTicket);