简体   繁体   English

基于 .NET Framework 构建的 .NET Core 2.1 Web API 上的授权问题

[英]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.我正在尝试让 [Authroize] 标记在基于 .NET Framework 构建的 .NET Core 2.1 Web API 项目上工作。 There is no IAppBuilder.UseAuthorization().没有 IAppBuilder.UseAuthorization()。 I wrote my own JWT custom auth handler and tried the following in the Startup:我编写了自己的 JWT 自定义身份验证处理程序,并在 Startup 中尝试了以下操作:

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

 services.AddAuthorization();

I also have我也有

app.UseAuthentication();

but, again, there is no app.UseAuthorization()但是,同样,没有 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.我希望能够添加 [Authorize] 属性(也带有角色)并能够访问我的控制器方法中的 User 对象。

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();不需要: app.UseAuthorization();

Sorry for the trouble.抱歉,添麻烦了。 Just couldn't figure out that I needed the scheme name in the authorize tag.只是不知道我需要授权标签中的方案名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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