2017-02-15 152 views
6

無可否認,這是第一次構建Asp.Net Core web API項目..一個需求是支持OAuth2。 Api和Identity服務器是兩個獨立的項目,都是從Asp.Net核心Empty模板開始的。使用Asp.Net Core授權WebAPI

身份服務器已啓動並正在運行,並且令牌通過資源所有者流提供。獲取令牌很好,範圍和相關的access_token細節看起來是正確的。

當我發出GET請求,我的資源終點,我會在第一以下...

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] 
     Request starting HTTP/1.1 GET http://localhost:12886/v1/mystuff 
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[2] 
     Successfully validated the token. 
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[3] 
     HttpContext.User merged via AutomaticAuthentication from authenticationScheme: Bearer. 
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[8] 
     AuthenticationScheme: Bearer was successfully authenticated. 
info: IdentityModel.AspNetCore.ScopeValidation.ScopeValidationMiddleware[0] 
     Scopes found on current principal: scope: stuffdetails, scope: stuffmover 
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[8] 
     AuthenticationScheme: Bearer was successfully authenticated. 
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] 
     Authorization was successful for user: 939d72dd-654c-447f-a65d-d0426b1eca59. 

所以,我可以告訴中間件驗證我的道理,閱讀範圍和認證令牌。 但是,在初始成功後立即獲得授權失敗。

info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2] 
     Authorization failed for user: 939d72dd-654c-447f-a65d-d0426b1eca59. 
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1] 
     Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'. 
info: Microsoft.AspNetCore.Mvc.ChallengeResult[1] 
     Executing ChallengeResult with authentication schemes(). 
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[13] 
     AuthenticationScheme: Bearer was forbidden. 
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2] 
     Executed action TestApi.StuffController.GetStuff (TestApi) in 32.4439ms 
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] 
     Request finished in 1207.1769ms 403 

這裏是什麼,我相信是在啓動時的相關位。

ConfigureServices ...

services.AddMvcCore() 
     .AddAuthorization(opts => 
      { 
       opts.AddPolicy("stuffdetails", 
        policy => policy.RequireClaim("stuffdetails")); 
      } 
     ) 
     .AddJsonFormatters(); 

services.AddOptions(); 

配置 - 請注意,我知道我的configOptions是正確的,因爲最初的令牌的挑戰是成功的。

var authServerOptions = new IdentityServerAuthenticationOptions 
{ 
    Authority = configOptions.Value.AuthServerSettings.AuthServerURI, 
    RequireHttpsMetadata = configOptions.Value.AuthServerSettings.RequiresHttpsMetaData, 
    ApiName = configOptions.Value.AuthServerSettings.ApiName, 
    AllowedScopes = configOptions.Value.AuthServerSettings.AllowedScopes, 
    SupportedTokens = IdentityServer4.AccessTokenValidation.SupportedTokens.Jwt, 
    AuthenticationScheme = "Bearer", 
    SaveToken = true, 
    ValidateScope = true 
}; 

app.UseIdentityServerAuthentication(authServerOptions); 
app.UseMvc(); 

東西控制器

[Route("v1/[controller]")] 
[Authorize(ActiveAuthenticationSchemes = "Bearer")] 
public class StuffController : Controller 
{ 
    [HttpGet] 
    [Authorize(Policy = "stuffdetails")] 
    public JsonResult GetStuff() 
    { 
     return new JsonResult(new 
     { 
      Message = "You've got stuff.." 
     }); 
    } 
} 

如果我刪除從GetStuff法的授權屬性,一切都很好,因爲作爲日誌表明,承載令牌授權。

的問題:

  1. 是授權失敗,因爲我的政策是不正確的,如果是的話它應該如何設置?
  2. 如果我想驗證一個令牌包含適當的聲明並且被授權,那麼使用我有的策略是否正確?
  3. 我是否在嘗試使用UseIdentityServerAuthentication而不是UseJwtBearerAuthentication

任何幫助是極大的讚賞..

+0

你在問三個很好的問題,儘管三個問題在一個。考慮把你的問題分成三個不同的問題。現在,它有點過於寬泛。 –

回答

2

是授權失敗,因爲我的政策是不正確的,如果是的話如何0​​它應該是設置?

你看起來是否正確,但你可以通過刪除Authorize屬性中的'policy'部分來輕鬆驗證:如果它現在可以工作,那麼問題是如果它仍然失敗那麼這是一個比你的政策更廣泛的問題。我假設你正在用你自己實現的IProfileService將「stuffdetails」聲明添加到你的access_token中?

如果我想驗證一個令牌包含正確的聲明,並且授權了 ,那麼使用像我一樣的策略是否正確?

是的,這似乎是做自定義授權的aspnet核心方式。

我在嘗試使用UseIdentityServerAuthentication 而不是UseJwtBearerAuthentication時犯了一個錯誤嗎?

我正在使用UseIdentityServerAuthenticationResourceOwnerPassword流量。我很想聽聽UseJwtBearerAuthentication方法是首選還是提供其他功能。

+0

+1 Mashton,感謝您的協助,在查看訪問令牌中的索賠時,我意識到我期待的是「stuffdetails」範圍,這就是爲什麼我的政策不起作用。 – jaeckyl

+0

很高興您發現問題,並且我能夠提供一些幫助你達到目標的指標。 – Mashton

1

在我的部分是我創造了我的政策方式的錯誤:

opts.AddPolicy("stuffdetails", 
          policy => policy.RequireClaim("stuffdetails")); 

應該是:

opts.AddPolicy("stuffdetails", 
          policy => policy.RequireClaim("scope","stuffdetails")); 

政策應該確認範圍包括「stuffdetails」。一個偉大的任何有問題的人的資源是damienbod的帖子,Authorization Policies and Data Protection with IdentityServer4 in ASP.Net Cord