简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM