首页 新闻 会员 周边

.net core2.0 cookies 老是过期 这是为什么

0
悬赏园豆:20 [待解决问题]

services.AddAuthentication(options => {
options.DefaultChallengeScheme = Constant.AuthenticationScheme;
options.DefaultSignInScheme = Constant.AuthenticationScheme;
options.DefaultAuthenticateScheme = Constant.AuthenticationScheme;
})
.AddCookie(Constant.AuthenticationScheme, m =>
{

//m.Cookie.HttpOnly = true;
//m.Cookie.SameSite = SameSiteMode.Lax;
//m.Cookie.SecurePolicy = CookieSecurePolicy.Always;
m.LoginPath = new PathString("/Account/Login");
m.AccessDeniedPath = new PathString("/Account/Denied");
m.LogoutPath = new PathString("/Account/Logout");
m.Cookie.Path = "/";
});

 

 

 

 

 

    app.UseAuthentication();

 

 

await HttpContext.SignInAsync(Constant.AuthenticationScheme, ClaimsPrincipal(r.User, ip),
new Microsoft.AspNetCore.Authentication.AuthenticationProperties
{

// 持久保存
//IsPersistent = true,

 

ExpiresUtc = DateTime.UtcNow.AddHours(24)


//// 指定过期时间
// ExpiresUtc = DateTime.UtcNow.AddHours(24),
// //ExpiresUtc = DateTime.UtcNow.AddHours(24),
// //IsPersistent = true,
// AllowRefresh = true
});

问题补充:

特地新建空项目测试  

 

// 添加 Cook 服务
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Home/Index";
options.LogoutPath = "/Home/Index";
});

 

// 使用Cook的中间件
app.UseAuthentication();

 

 

var user = new ClaimsPrincipal(
new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.Name, "username"),
},
CookieAuthenticationDefaults.AuthenticationScheme));

await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, user, new Microsoft.AspNetCore.Authentication.AuthenticationProperties
{
IsPersistent = true,
ExpiresUtc = DateTimeOffset.Now.Add(TimeSpan.FromDays(7)) // 有效时间
});

 

运行之后 看了 浏览器的 cookies  过期时间是  

 

2017-10-17T08:22:05.487Z

结果  不到一小时    就又退出了   查看浏览器  发现cookies 还在 

控制器验证  加    [Authorize] 这个的

程序员编程日记的主页 程序员编程日记 | 初学一级 | 园豆:6
提问于:2017-10-09 17:48
< >
分享
所有回答(5)
0

看响应报文里的setcookie头

吴瑞祥 | 园豆:29449 (高人七级) | 2017-10-09 17:52

浏览会话结束时  过期时间 为什么是这个

支持(0) 反对(0) 程序员编程日记 | 园豆:6 (初学一级) | 2017-10-09 18:04

@conan_lin: 看不懂.什么意思?

支持(0) 反对(0) 吴瑞祥 | 园豆:29449 (高人七级) | 2017-10-09 18:10
0

我们的项目中 SignInAsync 是这么写的

await context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
    claimsPrincipal,
    new AuthenticationProperties
    {
        IsPersistent = isPersistent,
        ExpiresUtc = DateTimeOffset.Now.Add(_cookieAuthOptions.ExpireTimeSpan)
    });
dudu | 园豆:30994 (高人七级) | 2017-10-09 18:22

我刚刚试了一下 ExpiresUtc = DateTime.UtcNow.AddMonths(1) 也是正常的

await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
    claimsPrincipal,
    new AuthenticationProperties
    {
        IsPersistent = true,
        ExpiresUtc = DateTime.UtcNow.AddMonths(1)
    });
支持(0) 反对(0) dudu | 园豆:30994 (高人七级) | 2017-10-09 20:54

@dudu: 

await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, ClaimsPrincipal(r.User, ip), new Microsoft.AspNetCore.Authentication.AuthenticationProperties
{
IsPersistent = true,
ExpiresUtc = DateTimeOffset.Now.Add(TimeSpan.FromDays(7)) // 有效时间
});

我是登录打开   放着不动    过差不多1个小时  在操作  发现退出   但是浏览器的cookies 的过期时间是没错的  7天之后 

支持(0) 反对(0) 程序员编程日记 | 园豆:6 (初学一级) | 2017-10-10 12:02
0

请问下你这个问题是如何解决的  我现在也遇到这个问题 本地开发没问题 发布部署到服务器就出现这个问题

EasyCms- | 园豆:120 (初学一级) | 2018-03-05 12:28

  自己写cookies  没有使用它的

支持(0) 反对(0) 程序员编程日记 | 园豆:6 (初学一级) | 2018-03-05 14:08

 https://gitee.com/conan5566linyiling/conan.net

支持(0) 反对(0) 程序员编程日记 | 园豆:6 (初学一级) | 2018-03-05 15:29
0

你用了IdentityServer4吧? client的 Token默认过期时间是一个小时,这个是在初始化clients中设置  

南昌炒粉 | 园豆:760 (小虾三级) | 2018-05-22 10:21
0

一样遇到这个问题。加时间感没用。cookie提交都是带上了的。

gw2010 | 园豆:1487 (小虾三级) | 2019-04-23 17:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册