如何保持一个用户登录2周? [英] How do I Keep a user logged in for 2 weeks?
问题描述
HI
我使用asp.net的MVC与asp.net成员。
I am using asp.net mvc with asp.net membership.
我想有一个复选框,如果单击该保持用户签订为期2周(除非他们明确自己的cookie)。
I want to have a checkbox that if clicked keeps the users signed in for 2 weeks(unless they clear their cookies).
所以我知道他们是
FormsAuthentication.SetAuthCookie(用户名,createPersistentCookie)
FormsAuthentication.SetAuthCookie(userName, createPersistentCookie)
但我不知道如何设置它为2周保留。
but I don't know how to set it up for 2week retention.
我重写了大部分会员的东西。所以我不喜欢使用的东西创建()和VerifyUser()。
I rewrote most of the membership stuff. So I don't use stuff like Create() and VerifyUser().
推荐答案
您可以设置在web.config中如全球会话超时(该值以分钟为单位)。
You can set the global session timeout (the value is in minutes) in web.config eg.
<system.web>
<authentication mode="Forms">
<forms timeout="20160"/>
</authentication>
</system.web>
这将是所有身份验证的用户。如果您想使用记住我功能,则需要编写自己的code设置cookie /票。事情是这样的(拍摄<一个href=\"http://www.dotnetspider.com/forum/201070-How-implement-remember-me-next-time-feature-for-login-control-asp-net.aspx\"相对=nofollow>从这里):
This will be for all authenticated users. If you want to use the 'Remember Me' functionality then you will need to write your own code to set the cookie/ticket. Something like this (taken from here):
protected void Page_Load()
{
if (Request.Cookies["username"] == null || Request.Cookies["username"].Value.ToString().Trim() == "")
{
Login1.RememberMeSet = true;
}
else
{
Login1.UserName = Request.Cookies["username"].Value.ToString().Trim();
Login1.RememberMeSet = true;
}
}
protected void RememberUserLogin()
{
// Check the remember option for login
if (Login1.RememberMeSet == true)
{
HttpCookie cookie = new HttpCookie("username");
cookie.Value = Login1.UserName.Trim();
cookie.Expires = DateTime.Now.AddHours(2);
HttpContext.Current.Response.AppendCookie(cookie);
Login1.RememberMeSet = true;
}
else if (Login1.RememberMeSet == false)
{
HttpContext.Current.Response.Cookies.Remove("username");
Response.Cookies["username"].Expires = DateTime.Now;
Login1.RememberMeSet = false;
}
}
这篇关于如何保持一个用户登录2周?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!