3

我使用 oidc-client 在我的应用程序中使用 angular 和 asp net core 3.1 进行授权

如何通过 oidc 客户端从 asp net 获取用户角色?

4

1 回答 1

2

身份:

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

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

        public async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            ApplicationUser user = await mUserManager.GetUserAsync(context.Subject);

            IList<string> roles = await mUserManager.GetRolesAsync(user);

            IList<Claim> roleClaims = new List<Claim>();
            foreach (string role in roles)
            {
                roleClaims.Add(new Claim(JwtClaimTypes.Role, role));
            }

            context.IssuedClaims.AddRange(roleClaims);
        }

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

启动.cs:

...

services.AddDefaultIdentity<ApplicationUser>(options => options.User.RequireUniqueEmail = true)
                .AddRoles<IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>();

services.AddIdentityServer()
                    .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

services.AddTransient<IProfileService, ProfileService>();

services.AddAuthentication()
                .AddIdentityServerJwt();

...

Angular oidc 客户端:

在此处输入图像描述

于 2020-06-04T08:02:37.730 回答