Robin
Robin

Reputation: 625

add multiple cookie schemes in aspnet core 2

How to add multiple cookie schemes in aspnet core 2.0?

I've followed instructions from here Auth 2.0 Migration announcement and here Migrating Authentication and Identity to ASP.NET Core 2.0 but i am unable to add multiple schemes.

for example:

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;
});

Upvotes: 2

Views: 4764

Answers (1)

Robin
Robin

Reputation: 625

Adding multiple schemes in aspnet core 2.0 is simple. I've solved by doing this.

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;
});

discussion can be found here Auth 2.0 Migration announcement

Upvotes: 9

Related Questions