【发布时间】:2020-10-08 08:42:56
【问题描述】:
我正在尝试学习身份服务器 4,除了我的 MVC 客户端中的角色授权之外,我的所有功能都在工作(它通过从我的 MVC 客户端调用的 JWT Bearer 在我的 Web API 中正常工作)。
几个小时以来,我一直在寻找并尝试实施潜在的修复,但我似乎没有取得任何进展,所以我认为我错过了这个难题的一个小而关键的部分。
这是我的 MVC 客户端 StartUp 的(部分):
.AddOpenIdConnect("oidc", options =>
{
options.Authority = appSettings.IdentityUrl;
options.RequireHttpsMetadata = false;
options.ClientId = "mvc";
options.ClientSecret = "secret";
options.ResponseType = "code id_token";
options.SaveTokens = true;
options.Scope.Add("web_api");
options.Scope.Add("offline_access");
// I was under the impression that was the 'magic' part which mapped claims to roles for use with the Authorize() attribute
options.ClaimActions.MapJsonKey("role", "role", "role");
}
这是我登录后获得的 access_token 的有效负载,如您所见,角色在那里:
{
"nbf": 1592473463,
"exp": 1592477063,
"iss": "https://localhost:5000",
"aud": "web_api",
"client_id": "mvc",
"sub": "b2657e83-4256-4fe2-86e9-3bfa53d462e2",
"auth_time": 1592473458,
"idp": "local",
"is_enabled": "True",
"role": [
"Player",
"Admin"
],
"scope": [
"openid",
"profile",
"web_api",
"offline_access"
],
"amr": [
"pwd"
]
}
返回拒绝访问的示例 MVC 控制器端点:
[HttpGet]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> AuthorisedOnly()
{
...SNIP...
return View("Access", vm);
}
我假设(可能是错误的)如果我的 access_token 中有角色,那么我的身份数据库和ProfileService 设置正确。 (我在ProfileService 的GetProfileDataAsync 中添加角色)。
欢迎提出任何建议,如果您需要有关我的设置的更多信息,请告诉我。我不想用大量不相关的配置数据来夸大这个问题。
【问题讨论】:
-
问题似乎出在映射上,请阅读this article。
标签: asp.net-mvc asp.net-core identityserver4 openid-connect