【问题标题】:Implement role based access control with IdentityServer4 and .NET Core使用 IdentityServer4 和 .NET Core 实现基于角色的访问控制
【发布时间】:2019-07-05 14:27:44
【问题描述】:

我们有一个现有的数据库,需要使用 IdentityServer4 和 .NET Core 实现基于角色的访问控制。

我的问题是:

1 IdenityServer4 是否支持 RBAC?例如是通过范围声明吗?应该如何实现?

2 我可以使用现有的数据库吗?或者我必须创建一个新数据库吗?

任何建议或代码示例将不胜感激。

https://github.com/IdentityServer/IdentityServer4.Samples/tree/master/Quickstarts/7_EntityFrameworkStorage

更新

我们实施资源所有者密码授权类型和 RBAC。

如有任何建议或代码示例链接,我们将不胜感激。

【问题讨论】:

  • 您可以查看github.com/IdentityServer/IdentityServer4/issues/1786 了解如何在服务器(IDS4)和客户端(MVC)端涉及角色声明。
  • 角色不是身份服务器的一部分,而是身份的一部分。从我读到的内容来看,他们正在远离 AspNetRoles 表。另一种方法是使用“角色”声明。在设置 IdentityServer 客户端/api 的配置时,您可以映射到代表角色的声明。
  • @RuardvanElburg 您能否详细说明“角色”声明或代码示例的链接。 “在设置 IdentityServer 客户端/api 的配置时,您可以映射到代表角色的声明”
  • github.com/IdentityServer/IdentityServer4.Samples/blob/… 这会将“角色”类型的声明映射为角色(可与 IsInRole 等一起使用)

标签: asp.net-core identityserver4


【解决方案1】:

在 Identity Server 端,您可以创建 Profile Service 以使 IDS4 在颁发令牌时包含 role 声明。

例如,如果使用 ASP.NET Identity 来管理用户/角色,您可以创建配置文件服务,例如:

public class MyProfileService : IProfileService
{
    public MyProfileService()
    { }

    public Task GetProfileDataAsync(ProfileDataRequestContext context)
    {
        var roleClaims = context.Subject.FindAll(JwtClaimTypes.Role);
        List<string> list = context.RequestedClaimTypes.ToList();
        context.IssuedClaims.AddRange(roleClaims);
        return Task.CompletedTask;
    }

    public Task IsActiveAsync(IsActiveContext context)
    {
        // await base.IsActiveAsync(context);
        return Task.CompletedTask;
    }
}

并在 Startup.cs 中注册:

services.AddTransient<IProfileService, MyProfileService>();

在客户端,您应该从您的 JWT 令牌映射角色声明,并在 AddOpenIdConnect 中间件中尝试以下配置:

  options.ClaimActions.MapJsonKey("role", "role", "role");
  options.TokenValidationParameters.RoleClaimType = "role";

【讨论】:

    猜你喜欢
    • 2020-02-28
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    • 2011-03-18
    • 2012-12-06
    • 2010-09-11
    • 1970-01-01
    相关资源
    最近更新 更多