简体   繁体   English

使用asp.net网页“记住我”

[英]“Remember Me” with asp.net web pages

I realize that this question may have been asked before, but I can't find anything that matches my situation exactly. 我意识到之前可能已经提出过这个问题,但我找不到任何与我的情况完全匹配的问题。

I created a website using the WebMail helper in ASP.Net web pages ( not web forms) and WebMatrix. 我在ASP.Net网页( 不是网络表单)和WebMatrix中使用WebMail帮助程序创建了一个网站。 Users are required to login to the website, and there is a "Remember me" box that (in theory) will keep the user logged in until he/she chooses to log out. 用户需要登录该网站,并且有一个“记住我”框(理论上)将保持用户登录,直到他/她选择退出。 The website does keep users logged in if they close the browser and reopen it within 20-30 minutes. 如果用户关闭浏览器并在20-30分钟内重新打开,该网站会让用户保持登录状态。 However, after 20-30 minutes of not accessing the website, the user is logged out. 但是,在不访问网站20-30分钟后,用户将被注销。 (As an aside, this problem seems to exist even with the WebMatrix template "Starter Site".) (顺便说一句,即使使用WebMatrix模板“Starter Site”,这个问题似乎也存在。)

I've tried multiple solutions, many of which were posted on Stack Overflow, but nothing seems to work. 我尝试了多种解决方案,其中许多都发布在Stack Overflow上,但似乎没有任何效果。

EDIT 2 编辑2

The cookie used by Forms Authentication is called ".ASPXAUTH" and by default set to expire after 30 minutes. 表单身份验证使用的cookie称为“.ASPXAUTH”,默认设置为30分钟后过期。

Go to your web.config and find the authentication element. 转到web.config并找到authentication元素。 You can set the cookie expiration time (in minutes) there, like such: 您可以在那里设置Cookie到期时间(以分钟为单位),如下所示:

<system.web>
    <authentication mode="Forms">
        <forms loginUrl="~/Account/Login" 
               name="myCookie"                  <!-- optional, if you want to rename it -->
               timeout="2880" />                <!-- expires in 48 hours -->
    </authentication>
</system.web>

OR 要么

If the config fails you, try this article: Link 如果配置失败,请尝试以下文章: 链接

You'll need to clear any existing auth tickets and create your custom one. 您需要清除任何现有的身份验证票并创建自定义票证。 It boils down to this piece of code you need to execute if the user selected the remember me option: 如果用户选择了remember me选项,它可以归结为您需要执行的这段代码:

    if (rememberMe)
    {
        // Clear any other tickets that are already in the response
        Response.Cookies.Clear(); 

        // Set the new expiry date - to thirty days from now
        DateTime expiryDate = DateTime.Now.AddDays(30);

        // Create a new forms auth ticket
        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, loginModel.UserName,  DateTime.Now, expiryDate, true, String.Empty);

        // Encrypt the ticket
        string encryptedTicket = FormsAuthentication.Encrypt(ticket);

        // Create a new authentication cookie - and set its expiration date
        HttpCookie authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
        authenticationCookie.Expires = ticket.Expiration;

        // Add the cookie to the response.
        Response.Cookies.Add(authenticationCookie);
    }

You can manually create a cookie(never expiring) containing a GUID which is mapped to your user. 您可以手动创建包含映射到您的用户的GUID的cookie(永不过期)。 When user makes a GET to your user login page, you can read that cookie and check the guid and authenticate the user. 当用户对您的用户登录页面进行GET时,您可以读取该cookie并检查guid并对用户进行身份验证。 check the links 检查链接

http://msdn.microsoft.com/en-us/library/ms178194(v=vs.100).aspx http://msdn.microsoft.com/en-us/library/ms178194(v=vs.100).aspx

http://msdn.microsoft.com/en-us/library/78c837bd(v=vs.100).aspx http://msdn.microsoft.com/en-us/library/78c837bd(v=vs.100).aspx

http://www.codeproject.com/Articles/31914/Beginner-s-Guide-To-ASP-NET-Cookies http://www.codeproject.com/Articles/31914/Beginner-s-Guide-To-ASP-NET-Cookies

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

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