雖然與AspNetAuthorization教程測試IdentityServer4我添加了一個簡單的[Authorize(Roles = "Administrator")]
從那以後,我得到這個錯誤:承載與授權禁止過濾器在IdentityServer4
AuthenticationScheme: Bearer was forbidden.
我的用戶有這種說法: new Claim(ClaimTypes.Role, "Administrator", ClaimValueTypes.String)
。
在ConfigureServices
方法:
services.AddAuthorization(options =>
{
options.AddPolicy("AdministratorOnly", policy => policy.RequireRole("Administrator"));
});
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
});
和Configure
方法:
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions()
{
Authority = "http://localhost:5000",
ScopeName = "openid",
AutomaticAuthenticate = true,
AutomaticChallenge = true,
RequireHttpsMetadata = false,
});
調試輸出:
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Debug: Executing action LearningEntityServer4.OAuth.ValuesController.Get (LearningEntityServer4.OAuth)
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService: Information: Authorization was successful for user: myuser.
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService: Information: Authorization failed for user: myuser.
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Warning: Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
Microsoft.AspNetCore.Mvc.ChallengeResult: Information: Executing ChallengeResult with authentication schemes().
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware: Information: AuthenticationScheme: Bearer was forbidden.
我錯過了在配置?
PS:我已經檢查過this SO帖子沒有成功。
我很感謝你的回答。人們無法使用寫得好的IdS和特別是IdS4的原因之一是缺乏廣泛的例子和文件來解決他們的問題。我被這個問題困住了好幾天。感謝您的廣泛解釋。 –