2017-08-03 108 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