"记住"用asp.net web页面 [英] "Remember Me" with asp.net web pages

查看:92
本文介绍了"记住"用asp.net web页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到这个问题可能已经问过,但我无法找到完全匹配我的情况什么。

I realize that this question may have been asked before, but I can't find anything that matches my situation exactly.

我创建使用ASP.Net Web页面的webmail帮手(的的网页形式)和WebMatrix的一个网站。用户需要登录到网站,并有一个记住我框是(理论上)将保持用户登录,直到他/她选择退出。该网站不保存登录的用户,如果他们关闭浏览器,20-30分钟内重新打开它。然而,20-30分钟未访问网站之后,用户将被注销。 (顺便说一句,这个问题似乎与WebMatrix的模板入门网站,甚至存在。)

I created a website using the WebMail helper in ASP.Net web pages (not web forms) and WebMatrix. 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. However, after 20-30 minutes of not accessing the website, the user is logged out. (As an aside, this problem seems to exist even with the WebMatrix template "Starter Site".)

我尝试过多种解决方案,其中许多是张贴在堆栈溢出,但似乎没有任何工作。

I've tried multiple solutions, many of which were posted on Stack Overflow, but nothing seems to work.

推荐答案

编辑2

表单验证使用的cookie被称为.ASPXAUTH的默认设置30分钟后到期。

The cookie used by Forms Authentication is called ".ASPXAUTH" and by default set to expire after 30 minutes.

转到的web.config 并找到验证元素。您可以设置cookie的过期时间(分钟)那里,像这样的:

Go to your web.config and find the authentication element. You can set the cookie expiration time (in minutes) there, like such:

<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>


如果该配置失败,你,试试这个文章:<一href=\"http://blogs.msdn.com/b/friis/archive/2010/08/18/remember-me-checkbox-does-not-work-with-forms-authentication.aspx\"相对=nofollow>链接

If the config fails you, try this article: Link

您将需要清除现有的身份验证票和创建自定义之一。它归结为这片$ C $的c您需要的,如果用户选择了记得我执行选项:

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:

    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);
    }

这篇关于&QUOT;记住&QUOT;用asp.net web页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆