C# .Net Core API 使用身份服务器 4 的授权路由



我有一个使用身份服务器 4 的 API,并且注册和登录路由有效。但是我保护的那些[Authorized]给我404有或没有授权标头。如果我从路由中删除[Authorized],它会得到正确的命中。可能有什么问题?

这是控制器:

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
namespace Security.API.Controllers
{
[Route("[controller]")]
public class AccountController : Controller
{
private readonly IAccountService accountService;
public AccountController(IAccountService accountService)
{
this.accountService = accountService;
}
[HttpGet("")]
public string Get()
{
return "You are seeing this because account controller is working fine!";
}
[Authorize]
[HttpGet("getauthorized")]
public string GetAuthorized()
{
return "This is authorized okay.";
}
...

第一条路线被击中,第二条路线没有

需要将 [Authorize] 属性放在信赖方上,表示正在调用 Web API 的 Web 应用。身份服务器通过令牌身份验证保护您的 Web API。

  • 在客户端应用程序 (RP( 上放置 [授权]
  • 它将被重定向到身份验证服务器登录/注册
  • 成功登录后,将被重定向到客户端应用程序
  • 在此处获取访问令牌并调用 API

最新更新