Keep user logged in to ASP.NET 5 website for long time

时间:2015-10-06 09:06:11

标签: c# asp.net asp.net-mvc asp.net-core

I want users who select the "remember me" option to stay logged in to my website for a long time (e.g. 3 months, or until they clear cookies). Currently I have to log in again every 20 mins or so.

I'm using ASP.NET 5 / vnext / mvc 6 (beta 7). My website is based on the code visual studio makes as a template project. I know other questions ask a similar thing, but I don't see how it applies in the new ASP.NET.

I have this code in my Startup.ConfigureServices method, but it doesn't seem to have an effect:

    services.ConfigureIdentityApplicationCookie(options =>
    {              
        options.ExpireTimeSpan = TimeSpan.FromDays(90);
        options.SlidingExpiration = true;
        options.AutomaticAuthentication = true;
        options.LoginPath = new PathString("/Account/Login");
        options.LogoutPath = new PathString("/Account/LogOff");
    });

thanks

EDIT

Using fiddler, the response from logging in contains this cookie data:

Response sent 642 bytes of Cookie data: Set-Cookie: .AspNet.Microsoft.AspNet.Identity.Application=CfDJ8P8cKnxL87ZMjh0duvm7eKbBbA_vf1ECr95KgPd4MNsKBj0_SljMLWLPNzNFIr4PQTG1ZjVyQ7cfFMEehcI5JZrOlVVHfZ_SD29jN1vdhsdUMPTysvhvo6RlnDHq5YwFdnTNqw-_ia4cGWk8Iw05PJHsQ0mws_e0DzWpX088kysJuU0LcNoyPA22nyMoGrK1RP1Bax_XwixdO6jLQx164lqRqVYi6ys3VVPJP0aLOg3w4CovxcAemgMQEhAcNUdP6Q0rnBmfBn7FZR_kNEgXoiMkNNgBDwUuVyiweU3fw5rzE-mmBPo2IYBJWRoaSzNLcUV5gSTpDT2n8IMh4nPlTzGrFIUgCpHDhpmXJJ3EneC5i-eVaLGeQG1FAIBZZ-oNlolwdkXi63bXpHuRME9cnYLTm3cDpfooXKq0_Rn7ls4lN-wCF5kGvz6ALruUaPWNERvcKlccix7o3B_-rj1q5yhn1bKO2vumArRaq-QpHb2djaN84IdFBOw1CSJLpeQKeP3qrdJD8-GYl6chvbJ4FbA; expires=Mon, 04 Jan 2016 10:40:35 GMT; path=/; httponly

You can see at the end the expire time is in 90 days, which is correct, yet it will still log me out in 20 mins or so.

2 个答案:

答案 0 :(得分:0)

也许您有密钥存储问题?检查您的日志是否正确存储了cookie加密密钥。如果应用程序无法保留密钥,则它们将在重新启动后重新生成,因此所有登录名都将失效。

https://docs.microsoft.com/pl-pl/aspnet/core/security/data-protection/configuration/default-settings?view=aspnetcore-2.1

要在IIS上启用user profile,请检查应用程序池设置。如果您托管在Windows 7上,则应用程序池的默认设置会混乱。

答案 1 :(得分:-2)

您可以通过多种方式将cookies添加到Cookie集合中。以下示例显示了两种编写cookie的方法:

Response.Cookies["userName"].Value = "patrick";
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);

HttpCookie aCookie = new HttpCookie("lastVisit");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);