C# .Net Core API 使用 Identity Server 4 的授权路由
C# .Net Core API Authorized route with Identity Server 4
我有一个 API,它正在使用 Identity Server 4 并且注册和登录路由有效。但是我用 [Authorized]
保护的那些给我 404
有或没有授权 header。如果我从路线中删除 [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 应用正在调用 Web apis。身份服务器通过令牌身份验证保护您的网络 api。
- 在客户端应用程序 (RP) 上放置 [授权]
- 它将被重定向到授权服务器login/register
- 登录成功后,会跳转到客户端
- 在这里获取访问令牌并调用您的 api
我有一个 API,它正在使用 Identity Server 4 并且注册和登录路由有效。但是我用 [Authorized]
保护的那些给我 404
有或没有授权 header。如果我从路线中删除 [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 应用正在调用 Web apis。身份服务器通过令牌身份验证保护您的网络 api。
- 在客户端应用程序 (RP) 上放置 [授权]
- 它将被重定向到授权服务器login/register
- 登录成功后,会跳转到客户端
- 在这里获取访问令牌并调用您的 api