环境:ASP.NET Core 2.0,带有cookie的身份。
In Startup.ConfigureServices()
有这样的:
services.ConfigureApplicationCookie(options => {
options.ExpireTimeSpan = TimeSpan.FromDays(14);
options.Cookie.Expiration = TimeSpan.FromDays(14);
});
第一个来自CookieAuthenticationOptions
。第二个是来自CookieBuilder
。文档还提到Microsoft.AspNetCore.Http.CookieOptions.Expires
(尽管它在 lambda 中不可用)。
这些有什么区别呢?在 Core2 中设置过期时间的正确方法是什么?
以下是我在我使用的测试应用程序中用来设置 cookie 过期时间的内容。
public class Startup
{
...
// This method gets called by the runtime. Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
...
... // before services.AddMvc();!
services.AddAuthentication().AddCookie(options => {
options.Cookie.Expiration = TimeSpan.FromDays(14);
options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict;
options.Cookie.Name = "MyCookieName";
options.LoginPath = "/Account/Login";
options.AccessDeniedPath = "/Account/Forbidden";
});
// OR Perhaps, this could be what you need
services.ConfigureApplicationCookie(options =>
{
options.Cookie.Expiration = TimeSpan.FromDays(150);
options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict;
options.Cookie.Name = "MyCookieName";
options.LoginPath = "/Account/Login";
options.AccessDeniedPath = "/Account/Forbidden";
});
...
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
... // before app.UseMvc();!
app.UseAuthentication();
// WAS -> app.UseCookieAuthentication();
...
}
...
}
我认为这应该让你朝着正确的方向前进。
这对我有用,我还没有注意到任何问题。不过,距 Core 2.0 RTM 发布仅几周时间。 :)
希望这可以帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)