Tamer Rifai
Tamer Rifai

Reputation: 177

Authorization on .NET Core 2.1 Web API built on .NET Framework problem

I am trying to get the [Authroize] tag working on .NET Core 2.1 Web API project built on .NET Framework. There is no IAppBuilder.UseAuthorization(). I wrote my own JWT custom auth handler and tried the following in the Startup:

 services.AddAuthentication("Basic")
        .AddScheme<BasicAuthenticationOptions, EdcAuthenticationHandler>("Basic", null);

 services.AddAuthorization();

I also have

app.UseAuthentication();

but, again, there is no app.UseAuthorization()

I want to be able to add [Authorize] attributes (with roles as well) and be able to access the User object inside my controller methods.

Any insight would be appreciated.

Upvotes: 0

Views: 744

Answers (1)

Tamer Rifai
Tamer Rifai

Reputation: 177

Turns out it was simple. I needed the authorization tag like this:

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]

And

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddScheme<BasicAuthenticationOptions, EdcAuthenticationHandler>(JwtBearerDefaults.AuthenticationScheme, null);

There was no need for: app.UseAuthorization();

Sorry for the trouble. Just couldn't figure out that I needed the scheme name in the authorize tag.

Upvotes: 2

Related Questions