Net Core 2.0 API setting up authorization and authentication

Viewed 6745

I have a .net core application that I have upgraded from 1.1 to 2.0. The problem I am having is working out how to set up both authentication and authorization.

I am getting this exception when I am trying to hit an api endpoint...

2017-08-15 15:28:12.2191|13|Microsoft.AspNetCore.Server.Kestrel|ERROR| Connection id "0HL73T7CAJGBE", Request id "0HL73T7CAJGBE:00000001": An unhandled exception was thrown by the application. No authenticationScheme was specified, and there was no DefaultChallengeScheme found.

My controller has this attribute on it...

[Authorize(Policy = "Viewer3AuthPolicy")]

My startup.cs has this method to try and set everything up...

   public void ConfigureServices(IServiceCollection services)
    {
    SetCorsPolicy(services);

    services.AddMvc();

    services.AddAuthentication(o =>
    {
        o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    });

    SetAuthorisationPolicy(services);

    services.AddAuthorization(options =>
    {
        options.DefaultPolicy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme).RequireAuthenticatedUser().Build();
        options.AddPolicy("Viewer3AuthPolicy",
            policy => policy.RequireClaim(Constants.AuthPolicyClaimsName, Constants.AuthPolicyClaimsValue));
    });
}

In my Configuremethod I am calling...

app.UseAuthentication();

I am thinking that I must have some ordering wrong or am making the wrong calls in the setup.

Does anyone have any ideas?

2 Answers

Adding

AddJwtBearer(options => { options.RequireHttpsMetadata = false; ... });"

did the trick for me.

[dotnet core 2.0]

Related