Skip to content

Instantly share code, notes, and snippets.

@GeorgDangl
Created November 8, 2017 20:53
Show Gist options
  • Save GeorgDangl/dffdcd69090cc939575a45cf91d18553 to your computer and use it in GitHub Desktop.
Save GeorgDangl/dffdcd69090cc939575a45cf91d18553 to your computer and use it in GitHub Desktop.
Challenging multiple authentication schemes in Asp.Net Core 2.0 with a default policy on all actions
services.AddIdentity<AppUser, AppRole>();
services.AddAuthentication()
.AddIdentityServerAuthentication(o =>
{
o.RequireHttpsMetadata = !environment.IsDevelopment();
o.ApiName = "api";
o.Authority = configuration["IdentityServer:Authority"];
});
services.AddMvc(options =>
{
// The default policy is to make sure that both authentication schemes - Cookie and Jwt - are challenged
var defaultPolicy = new AuthorizationPolicyBuilder(IdentityConstants.ApplicationScheme, "Bearer")
.RequireAssertion(c => true) // A requirement is mandatory
.Build();
options.Filters.Add(new AuthorizeFilter(defaultPolicy));
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment