c# - 配置 Identity Server 以使用 ASP.NET Identity 角色

标签 c# asp.net-identity identityserver4

我正在尝试了解有关 Identity Server 的更多信息。我目前正在努力让基于角色的授权发挥作用。以下是我遵循的步骤:

1) 下载示例解决方案:https://github.com/IdentityServer/IdentityServer4.Samples/tree/release/Quickstarts/6_AspNetIdentity

2) 运行解决方案,开始:

a) Identity Server 项目

b) MVC 项目

c) API 项目

3) 浏览到 MVC 项目并应用迁移。 4)注册新用户:[email protected] 5) 浏览到:MVC 项目中的 CallApiUsingUserAccessToken。由于用户已获得授权,因此可以按预期访问 API。

假设我现在想更改 IdentityContoller:

[Authorize] 
public class IdentityController : ControllerBase 

对此:

[Authorize(Roles="Admin")] 
public class IdentityController : ControllerBase 

和家庭 Controller ( 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";

请假设我已将角色正确添加到身份数据库(并将角色与用户相关联)。

最佳答案

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

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

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>();

关于c# - 配置 Identity Server 以使用 ASP.NET Identity 角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53194947/

相关文章:

C# Interop.Word 从项目资源文件夹添加图像

c# - 当输入为 IQueryable 时,无法让 Automapper 返回派生类型

asp.net-mvc - 阻止未确认电子邮件的用户登录 ASP.NET MVC Web API Identity(OWIN 安全)

c# - Intranet 站点的 ASP.NET Identity + Windows 身份验证

c# - 身份服务器 4 不显示所有用户声明

c# - IdentityServer 4,为 CRUD 客户端创建面板

asp.net-core-2.0 - Identity Server 4 中的 Refreshtoken 为 null

c# - 如何仅对选定类型的数据库使用 FluentMigrator.Runner

c# - 从 ADO.NET 迁移到 ADO.NET Entity Framework

c# - Asp.net Identity 2.0 临时密码