简体   繁体   English

如何让用户保持登录状态 2 周?

[英]How do I Keep a user logged in for 2 weeks?

HI你好

I am using asp.net mvc with asp.net membership.我正在使用具有 asp.net 会员资格的 asp.net mvc。

I want to have a checkbox that if clicked keeps the users signed in for 2 weeks(unless they clear their cookies).我想要一个复选框,如果点击该复选框,用户可以登录 2 周(除非他们清除了他们的 cookie)。

So I know their is所以我知道他们是

FormsAuthentication.SetAuthCookie(userName, createPersistentCookie) FormsAuthentication.SetAuthCookie(userName, createPersistentCookie)

but I don't know how to set it up for 2week retention.但我不知道如何将其设置为保留 2 周。

I rewrote most of the membership stuff.我重写了大部分会员内容。 So I don't use stuff like Create() and VerifyUser().所以我不使用像 Create() 和 VerifyUser() 这样的东西。

Add a hash key or a random string to both the cookie and the database (both the same key).将散列键或随机字符串添加到 cookie 和数据库(都相同的键)。 If the cookie and database value are the same, when the user starts a new session, sign him/her in again.如果 cookie 和数据库值相同,则当用户开始新会话时,重新登录他/她。 When the user reaches the two weeks, remove the secret key from the database using a cronjob (Unix) or scheduled task (Windows).当用户达到两周时,使用 cronjob (Unix) 或计划任务 (Windows) 从数据库中删除密钥。

Warning: Do not rely on the cookie expire date, since people can hack their browser.警告:不要依赖 cookie 的过期日期,因为人们可以破解他们的浏览器。
Rule: NEVER, EVER trust ANY of your users!规则:永远,永远不要相信你的任何用户!

You can set the global session timeout (the value is in minutes) in web.config eg.您可以在 web.config 中设置全局会话超时(值以分钟为单位),例如。

<system.web>
    <authentication mode="Forms">
          <forms timeout="20160"/>
    </authentication>
</system.web>

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.如果您想使用“记住我”功能,那么您需要编写自己的代码来设置 cookie/ticket。 Something like this (taken from here ):像这样的东西(取自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 周到期日期的简单 cookie。

Have you seen this?你见过这个吗?

http://forums.asp.net/t/1440824.aspx http://forums.asp.net/t/1440824.aspx

Along similar lines to what Koning has suggested.与科宁的建议类似。

You can not use a session method to keep your users logged in, since browsers delete the session cookies when the browser is closed.您不能使用会话方法来保持用户登录,因为浏览器会在浏览器关闭时删除会话 cookie。

Do what user142019 offered and set the session's IdleTimeout parameter very short, up to 15 min.执行 user142019 提供的操作并将会话的 IdleTimeout 参数设置得非常短,最多 15 分钟。 When the server receives any request from the browser, first check the session if it's alive.当服务器收到来自浏览器的任何请求时,首先检查会话是否处于活动状态。 if not, try to get the cookie.如果没有,请尝试获取 cookie。 If the cookie and database value are the same and not expired, assign it to the (new) session and return the response.如果 cookie 和数据库值相同且未过期,则将其分配给(新)会话并返回响应。

You can use onBeforeUnload listener to send a logout request when the user leaves your site.您可以使用 onBeforeUnload 侦听器在用户离开您的站点时发送注销请求。 If logged out, delete the cookie and the db record, if not - assign a new hash for the next auto login and refresh that hash again when the user retunes to your website.如果注销,请删除 cookie 和 db 记录,如果没有 - 为下一次自动登录分配一个新的哈希值,并在用户重新调整到您的网站时再次刷新该哈希值。 You can also keep track of IP and the browser and link them to the hash in your db.您还可以跟踪 IP 和浏览器并将它们链接到您的数据库中的哈希值。 So, in case if the cookie is used with another browser or IP, and the hash code is valid, you can force them to login again.因此,如果 cookie 与其他浏览器或 IP 一起使用,并且哈希码有效,您可以强制他们再次登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM