首页 新闻 会员 周边 捐助

在ASP.NET Core 2.0里,使用services.AddCookieAuthentication身份验证,设置过期时间不起作用怎么办?

0
悬赏园豆:10 [已解决问题] 解决于 2017-05-20 14:24
在使用ASP.NET Core 2.0的时候使用下面的代码实现身份验证,设置了ExpireTimeSpan不起作用始终是会话结束过期
services.AddCookieAuthentication(options =>
{
    options.ExpireTimeSpan = TimeSpan.FromDays(2);
});
services.AddMvc(options =>
{
    var policy = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .Build();

    options.Filters.Add(new AuthorizeFilter(policy));
});
三大神捕之四的主页 三大神捕之四 | 初学一级 | 园豆:55
提问于:2017-05-17 22:19
< >
分享
最佳答案
3

还需要在登录时设置AuthenticationProperties,参考代码:

var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, loginName) }, "Basic");
var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
await context.Authentication.SignInAsync(_cookieAuthOptions.AuthenticationScheme,
    claimsPrincipal,
    new AuthenticationProperties
    {
        IsPersistent = isPersistent,
        ExpiresUtc = DateTimeOffset.Now.Add(_cookieAuthOptions.ExpireTimeSpan)
    });
收获园豆:10
dudu | 高人七级 |园豆:30925 | 2017-05-18 09:11

await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.Now.AddDays(7) });

还是无效呀

Payson | 园豆:204 (菜鸟二级) | 2019-08-18 23:42
其他回答(1)
0

await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.Now.AddDays(7) });

还是无效呀

Payson | 园豆:204 (菜鸟二级) | 2019-08-18 23:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册