Asp.Net Core 3身份-来自浏览器的JWT中不存在自定义声明

问题描述 投票:0回答:1

Asp.Net Core 3.0我正在使用ASP.NET Core web application with Angular and Authentication(单个用户帐户)模板(来自Visual Studio 2019)。我的意图是在生成的JWT中添加一些自定义声明,并在浏览器中使用它们。为此,我扩展了UserClaimsPrincipalFactory

public class MyCustomClaimsInjector : UserClaimsPrincipalFactory<ApplicationUser>
{
    public MyCustomClaimsFactory(UserManager<ApplicationUser> userManager, IOptions<IdentityOptions> optionsAccessor) : base(userManager, optionsAccessor)
    {
    }

    protected override async Task<ClaimsIdentity> GenerateClaimsAsync(ApplicationUser user)
    {
        var id = await base.GenerateClaimsAsync(user);
        id.AddClaim(new Claim("my_claim1", "AdditionalClaim1"));
        id.AddClaim(new Claim("my_claim2", "AdditionalClaim2"));
        return id;
    }
}

同样,我已经在Startup.cs中注册了扩展名

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddDefaultIdentity<ApplicationUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddClaimsPrincipalFactory<MyCustomClaimsFactory>();


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

        services.AddAuthentication()
            .AddIdentityServerJwt();
        services.AddControllersWithViews();
        services.AddRazorPages();
        // In production, the Angular files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist";
        });
    }

在登录阶段,从SPA客户端开始,调试器通过MyCustomClaimsFactory,然后以ClaimsIdentity方法将声明添加到GenerateClaimsAsync

但是,我感到奇怪的是,为什么浏览器中收到的JWT不包含MyCustomClaimsFactory添加的声明。

我期望在浏览器的JWT中看到自定义声明,确定吗?

[谁能建议挖掘的方向...为什么JWT中没有索赔?

解码后的JWT是:enter image description here

SPA应用程序:enter image description here

c# authentication jwt asp.net-core-identity asp.net-core-3.0
1个回答
0
投票

将分享我的结果。希望对其他人有帮助。

我已经实现了IProfileService,并在.AddProfileService<ProfileService>()中通过管道传递了ConfigureServices实现。

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 claims = new List<Claim>
        {
            new Claim("my_FirstName", "user_FirstName"),
            new Claim("my_LastName", "user_LastName")
        };

        context.IssuedClaims.AddRange(claims);
    }

    public async Task IsActiveAsync(IsActiveContext context)
    {            
        var user = await _userManager.GetUserAsync(context.Subject);

        context.IsActive = (user != null);
    }
}

Startup.cs文件

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

这样,现在JWT包含了我的自定义声明。我不确定为什么UserClaimsPrincipalFactory的替代无法解决该问题。将尝试对这些领域进行更深入的研究。

© www.soinside.com 2019 - 2024. All rights reserved.