在aspnet core 2中添加多个cookie方案

时间:2017-08-22 05:45:02

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

如何在aspnet core 2.0中添加多个Cookie方案?

我已按照此处的说明Auth 2.0 Migration announcement 在这里Migrating Authentication and Identity to ASP.NET Core 2.0 但我无法添加多个方案。

例如

services.AddAuthentication("myscheme1").AddCookie(o =>{
        o.ExpireTimeSpan = TimeSpan.FromHours(1);
        o.LoginPath = new PathString("/forUser");
        o.Cookie.Name = "token1";
        o.SlidingExpiration = true;
});

services.AddAuthentication("myscheme2").AddCookie(o =>{
        o.ExpireTimeSpan = TimeSpan.FromHours(1);
        o.LoginPath = new PathString("/forAdmin");
        o.Cookie.Name = "token2";
        o.SlidingExpiration = true;
});

1 个答案:

答案 0 :(得分:4)

aspnet core 2.0中添加多个方案很简单。 我已经解决了这个问题。

services.AddAuthentication()
.AddCookie("myscheme1", o => // scheme1
{
        o.ExpireTimeSpan = TimeSpan.FromHours(1);
        o.LoginPath = new PathString("/forUser");
        o.Cookie.Name = "token1";
        o.SlidingExpiration = true;
})
.AddCookie("myscheme2", o => //scheme2
{
        o.ExpireTimeSpan = TimeSpan.FromHours(1);
        o.LoginPath = new PathString("/forAdmin");
        o.Cookie.Name = "token2";
        o.SlidingExpiration = true;
});

讨论可以在Auth 2.0 Migration announcement

找到