简体   繁体   English

像MVC一样授权WebAPI

[英]Authorization WebAPI behaving like MVC

I am using asp.core 2.1 to create web API's I have a controller like this 我正在使用asp.core 2.1创建Web API,我有一个像这样的控制器

 [Route("api")]
 [ApiController]
 public class LessonController : ControllerBase
 {

    [HttpGet]
    [Route("lessons/{id}")]
    [Authorize(Roles = "Teacher")]
    public async Task<IActionResult> GetLesson(int id)
    {
      //....
    }
 }

With the [Authorize] attribute i just get the error below? 使用[Authorize]属性,我只会看到以下错误?

The default Identity UI layout requires a partial view '_LoginPartial' usually located at '/Pages/_LoginPartial' or....

As this is An API I'm confused as to why in the error it is looking for partial views? 由于这是一个API,我很困惑为什么在错误中它正在寻找局部视图?

The error message you refer to comes from ASP.NET Core Identity's Default UI (specifically, it's in the _Layout.cshtml page here ). 你指的错误消息来自ASP.NET核心身份的默认UI(具体而言,它在_Layout.cshtml页面这里 )。 The Default UI is used when you use either of the following options in Startup.ConfigureServices : 当您在Startup.ConfigureServices使用以下任一选项时,将使用默认UI:

services.AddDefaultIdentity<User, Role>()
    ...

-or- -要么-

services.AddIdentity<User, Role>()
    .AddDefaultUI()
    ...

If you don't want to use the Default UI, you'll need to avoid using AddDefaultIdentity and AddDefaultUI and just use AddIdentity<User, Role> . 如果您不想使用默认UI,则需要避免使用AddDefaultIdentityAddDefaultUI而只需使用AddIdentity<User, Role>

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

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