We have a aspnetcore 2.0 website. The majority of the site is WebAPI, with 2 UI components: swagger and hangfire dashboard.
We are trying to secure the Web API endpoints using JWT and the UI (hangfire dashboard) components with Open ID.
here is our setup
services
.AddSingleton<IConfigureOptions<JwtBearerOptions>, ConfigureJwtBearerOptions>()
.AddSingleton<IConfigureOptions<OpenIdConnectOptions>, ConfigureOpenIdOptions>()
.AddAuthentication(options =>
{
var openId = OpenIdConnectDefaults.AuthenticationScheme;
var jwt = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = jwt;
//options.DefaultAuthenticateScheme = openId;
})
.AddJwtBearer()
.AddCookie()
.AddOpenIdConnect(options =>
{
var cookies = CookieAuthenticationDefaults.AuthenticationScheme;
options.SignInScheme = cookies;
options.SignOutScheme = cookies;
});
services
.AddAuthorization(options => options.AddPolicy(...))
.AddMvcCore(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
});
....
app.Map(new PathString("/hangfire"), _ => _.UseMiddleware<HangfireDashboardMiddleware>(...));
To secure the hangfire dashboard we had to create a delegation handler to authenticate and challenge using OpenIdConnectDefaults.AuthenticationScheme
.
The problem is we can only get JWT or OpenID to successfully authorize and get all the relevant claims.
When //options.DefaultAuthenticateScheme = openId;
is commented out JWT works, but OpenId gets stuck in a perpetual loop between localhost and AAD.
When options.DefaultAuthenticateScheme = openId;
is set Open ID authentication works properly, but JWT fails to get claims. It does appear to authenticate.
How can we setup Authentication scheme per route?