【发布时间】:2011-06-18 13:54:58
【问题描述】:
我正在使用 ASP.NET 表单身份验证将用户登录到我们正在开发的网站。
部分功能是“记住我”复选框,如果用户选中它,它会记住用户一个月。
用户登录代码如下:
public static void Login(HttpResponse response, string username,
bool rememberMeChecked)
{
FormsAuthentication.Initialize();
FormsAuthenticationTicket tkt = new FormsAuthenticationTicket(1, username, DateTime.Now,
DateTime.Now.AddMinutes(30), rememberMeChecked,
FormsAuthentication.FormsCookiePath);
HttpCookie ck = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(tkt));
ck.Path = FormsAuthentication.FormsCookiePath;
if (rememberMe)
ck.Expires = DateTime.Now.AddMonths(1);
response.Cookies.Add(ck);
}
web.config 中的相关部分是这样的:
<authentication mode="Forms">
<forms loginUrl="Home.aspx" defaultUrl="~/" slidingExpiration="true" timeout="43200" />
</authentication>
这会很好地记录用户,但如果他们不使用该站点,则会在半小时后将其注销,尽管其持久性属性 (rememberMeChecked) 设置为 true,如果为 true,则 cookie 设置为在月。我这里有什么遗漏吗?
提前致谢, F
【问题讨论】:
-
我不确定这是否会对这种情况产生影响,但是,使用
FormsAuthentication.RedirectFromLoginPage(userName, rememberMe)有什么问题?是否需要手动创建票证?如果您在配置中指定超时,那么您不需要在代码中手工制作它,AFAIK。另外,rememberMe设置在哪里?
标签: c# forms-authentication remember-me