Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 2.0 - AddAuthentication and DefaultScheme

I'm trying to upgrade an ASP.NET Core 1.1 application to 2.0. The application needs two both the basic authentication and JWT one. I have got code which looks something like this:

services.AddAuthentication(options =>
                    {
                        options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
                    })
                .AddBasic(BasicScheme, _ => { })
                .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
                {...}

My problem is that I can't seem to get both of them to work at the same time! Whichever one I set as the DefaultScheme is the only one that works and the other one breaks and returns a HTTP 401.

Any ideas how can I get both of them to work?

like image 215
Ambuj Avatar asked Jan 03 '23 21:01

Ambuj


2 Answers

Just managed to get this to work... this link helped: https://wildermuth.com/2017/08/19/Two-AuthorizationSchemes-in-ASP-NET-Core-2

The important bit is here:

When we use the Authorize attribute, it actually binds to the first authentication system by default. The trick is to change the attribute to specify which auth to use:

like image 156
Ambuj Avatar answered Jan 05 '23 13:01

Ambuj


Adding this line of code to Startup.cs worked for me

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddCookie("LoggedIn");
like image 21
Victor.Uduak Avatar answered Jan 05 '23 12:01

Victor.Uduak