【发布时间】:2019-04-11 04:57:18
【问题描述】:
我正在尝试了解有关 Identity Server 的更多信息。我目前正在努力使基于角色的授权正常工作。以下是我遵循的步骤:
1) 下载示例解决方案:https://github.com/IdentityServer/IdentityServer4.Samples/tree/release/Quickstarts/6_AspNetIdentity
2) 运行解决方案,该解决方案开始:
a) 身份服务器项目
b) MVC 项目
c) API 项目
3) 浏览到 MVC 项目并应用迁移。 4)注册新用户:Bert@Bert.com 5)浏览到:MVC项目中的CallApiUsingUserAccessToken。 API 已按预期访问,因为用户已获得授权。
假设我现在想从这里更改 IdentityContoller:
[Authorize]
public class IdentityController : ControllerBase
到这里:
[Authorize(Roles="Admin")]
public class IdentityController : ControllerBase
public async Task<IActionResult> CallApiUsingUserAccessToken()
到这里:
[Authorize(Roles="Admin")]
public async Task<IActionResult> CallApiUsingUserAccessToken()
我必须对配置进行哪些更改?
今天下午我尝试了一些建议。例如,在 MVCClient 的启动中,我尝试添加:
options.ClaimActions.MapJsonKey("role", "role", "role");
options.TokenValidationParameters.NameClaimType = "name";
options.TokenValidationParameters.RoleClaimType = "role";
请假设我已将角色正确添加到身份数据库(并将角色与用户关联)。
【问题讨论】:
-
在您的实施过程中,您需要从数据库中读取角色并将其添加到您的 ClaimsIdentity 中,类型为 ClaimType.Role
-
@ste-fu,你能发表一个答案吗?
标签: c# asp.net-identity identityserver4