【问题标题】:Configuring Identity Server to use ASP.NET Identity roles配置 Identity Server 以使用 ASP.NET Identity 角色
【发布时间】: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 

和家庭控制器 (https://github.com/IdentityServer/IdentityServer4.Samples/blob/release/Quickstarts/6_AspNetIdentity/src/MvcClient/Controllers/HomeController.cs) 来自:

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


【解决方案1】:

您正在寻找的是 AddProfileService() 方法,您可以在其中添加 IProfileService 接口的自定义实现,您可以自定义声明以添加到访问令牌。

这是一个使用 Identity 的示例,它将角色声明添加到令牌中

public class ProfileService : IProfileService
{
    protected UserManager<ApplicationUser> _userManager;

    public ProfileService(UserManager<ApplicationUser> userManager)
    {
        _userManager = userManager;
    }

    public async Task GetProfileDataAsync(ProfileDataRequestContext context)
    {
        var user = await _userManager.GetUserAsync(context.Subject);
        var roles = await _userManager.GetRolesAsync(user);
        var claims = new List<Claim>
        {
            new Claim(JwtClaimTypes.Role, roles.Any() ? roles.First() : "Standard")
        };

        context.IssuedClaims.AddRange(claims);
    }

    public async Task IsActiveAsync(IsActiveContext context)
    {
        var user = await _userManager.GetUserAsync(context.Subject);
        context.IsActive = (user != null) && user.LockoutEnabled;
    }
}

然后在启动时,告诉 idp 使用你的类

var builder = services.AddIdentityServer(options =>
                {
                    options.Events.RaiseErrorEvents = true;
                    options.Events.RaiseInformationEvents = true;
                    options.Events.RaiseFailureEvents = true;
                    options.Events.RaiseSuccessEvents = true;
                })
                .AddInMemoryIdentityResources(Config.GetIdentityResources())
                .AddInMemoryApiResources(Config.GetApiResources())
                .AddInMemoryClients(Config.GetClients())
                .AddAspNetIdentity<ApplicationUser>()
                .AddProfileService<ProfileService>();

【讨论】:

  • 这个类和配置是否需要同时添加到 MVC 项目和 API 项目中?谢谢。
  • @w0051977 您只需将其添加到存储用户的 Identity Server 项目中。通过 idp 登录的消费者将从令牌中获取角色,然后您将能够在控制器上使用基于角色的授权
  • Profile Service 类中存在错误:可访问性不一致 UserManager 。有什么想法吗?
  • @w0051977 这是一个更好的演练hamidmosalla.com/2017/12/07/…
猜你喜欢
  • 2020-12-11
  • 2016-02-28
  • 2013-12-26
  • 2015-06-13
  • 2021-07-18
  • 2019-09-20
  • 1970-01-01
  • 1970-01-01
  • 2016-10-24
相关资源
最近更新 更多