2017-08-03 111 views
1

我有一个使用Identity Server 4并注册和登录路由的API。但是我用[Authorized]保护的那些给我404有或没​​有授权标头。如果我从路线中删除[Authorized],它会被击中。可能是什么问题?C#.Net核心API使用Identity Server 4的授权路由

这是控制器:

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."; 
     } 
... 

首先路由中得到的命中,第二个没有

回答

2

你需要把[授权]属性上依赖方,意味着你的web应用程序是调用web apis。身份认证服务器通过令牌认证保护您的网站apis。

  • 认沽[授权]上的客户端应用程序(RP)
  • 将被重定向到auth服务器登录/注册
  • 成功登录后,将被重定向到客户端应用程序
  • 这里获得进入令牌,并呼吁你的API
+0

那么它应该是这样的未来,但这是为了测试目的。无论如何不应该击中路线?我的意思是,路由不知道它是客户端还是服务器,它听取我指定的请求:) – Norgul

+0

实际上它有点知道,因为我们必须以不同的方式配置中间件,如果它的客户端或Web API – JayDeeEss