[英]Authorization with Asp.Net Core WebAPI
不可否認,這是建立Asp.Net Core web api項目的第一步。一個要求是支持OAuth2。 Api和Identity服務器是兩個獨立的項目,都是從Asp.Net核心空模板開始的。
身份服務器已啟動並正在運行,並且正在通過資源所有者流提供令牌。 獲取令牌很好,范圍和相關的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方法中刪除Authorize屬性,一切都很好,因為如日志所示,持有者令牌被授權。
問題:
任何幫助是極大的贊賞..
授權是否失敗,因為我的政策不正確,如果是這樣,應如何設置?
您所看到的內容看起來是正確的,但您只需刪除“授權”屬性的“政策”部分即可輕松驗證:如果它現在有效,則問題與您的政策有關,如果仍然失敗那么這是一個更廣泛的問題而不僅僅是你的政策。 我假設您使用自己的IProfileService
實現將“stuffdetails”聲明添加到access_token中?
如果我想驗證令牌包含正確的聲明並獲得授權,那么使用我的策略是否正確?
是的,似乎是aspnet進行自定義授權的核心方式。
我是否在嘗試使用UseIdentityServerAuthentication而不是UseJwtBearerAuthentication時犯了錯誤?
我正在使用UseIdentityServerAuthentication
和ResourceOwnerPassword
流。 我有興趣聽聽UseJwtBearerAuthentication
方法是首選還是提供其他功能。
我的錯誤是我創建政策的方式:
opts.AddPolicy("stuffdetails",
policy => policy.RequireClaim("stuffdetails"));
應該:
opts.AddPolicy("stuffdetails",
policy => policy.RequireClaim("scope","stuffdetails"));
該策略應該確認范圍包括“stuffdetails”..對於任何遇到麻煩的人來說,一個很好的資源是在db.Net Cord中使用IdentityServer4的damienbod, 授權策略和數據保護的帖子
此外,您可以利用Microsoft.Aspnetcore.Authorization實現此功能。 主要的好處是它更易於閱讀,它允許您指定多個范圍(union /或)。
opts.AddPolicy("stuffdetails", ScopePolicy.Create("stuffdetails", "stuffdetails2"));
代替
opts.AddPolicy("stuffdetails",
policy => policy.RequireClaim("scope","stuffdetails"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.