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?
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:
Adding this line of code to Startup.cs worked for me
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddCookie("LoggedIn");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With