【问题标题】:How to add claims to my accesstoken generated by IdentityServer4 using ClientCredentials grantType如何使用 ClientCredentials grantType 向 IdentityServer4 生成的我的访问令牌添加声明
【发布时间】:2020-08-06 21:00:19
【问题描述】:

我开发了一个 WebAPI 应用程序并使用 IdentityServer4 使用 OAuth 2.0 协议保护我的端点

我的 ApiResource 看起来像:

                     Name = "BankOfDotNetApi",
                     Scopes =
                     {
                        new Scope("BankOfDotNetApi", "API name for Customer", new List<string>{ "Claim1"}),
                        new Scope("BankOfDotNetApi.Read"),
                        new Scope("BankOfDotNetApi.Write"),
                        new Scope("offline_access"),
                    },
                    UserClaims =
                    {
                        JwtClaimTypes.Name,
                        JwtClaimTypes.Email
                    },

MyClient 看起来像:

                Client
                {
                    ClientId = "client",
                    AllowedGrantTypes = GrantTypes.ClientCredentials,
                    ClientSecrets = {new Secret("secret".Sha256())},
                    AllowedScopes = { "BankOfDotNetApi", "BankOfDotNetApi.Read" },
                }

我的 API 应用 startUp.cs 看起来像:

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc(
                config =>
                {
                });

            services.AddControllers();
            services.AddDbContext<BankContext>(options => options.UseInMemoryDatabase("BankingDb"));

            services.AddAuthentication("Bearer")
                     .AddIdentityServerAuthentication(options =>
                     {
                         options.RequireHttpsMetadata = false;
                         options.ApiName = "BankOfDotNetApi";
                         options.Authority = "http://localhost:5000";
                     });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthentication();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }

我没有手动生成令牌(通过创建 JWTToken 的实例)并且令牌是由 IdentityServer4 自动生成的

我可以访问我的访问令牌中的范围,但我无法访问声明。
如果我的代码出错,请向我建议如何以及在何处向我的 ApiResource 添加声明。
如何访问我的 AccessToken 中的声明

【问题讨论】:

标签: c# asp.net-mvc asp.net-core .net-core identityserver4


【解决方案1】:

使用ICustomTokenRequestValidator接口,令牌生成后,控制流进入ValidateAsync方法。

namespace IdentityServer4.Validation
{
    //
    // Summary:
    //     Allows inserting custom validation logic into authorize and token requests
    public interface ICustomTokenRequestValidator
    {
        //
        // Summary:
        //     Custom validation logic for a token request.
        //
        // Parameters:
        //   context:
        //     The context.
        //
        // Returns:
        //     The validation result
        Task ValidateAsync(CustomTokenRequestValidationContext context);
    }
}

使用下面的行在令牌中添加自定义声明。

context.Result.ValidatedRequest.ClientClaims.Add(claim);

在启动类中使用 AddCustomTokenRequestValidator 添加自定义授权请求验证器。

【讨论】:

  • 感谢您的回复..但是需要在 ApiResource 模型中添加声明并通过 Token 访问..但是根据您的建议,我在哪里可以获得上下文对象。而我的方案是从 Token 获得声明,而不是向 Token 添加声明!.. 你能详细回答一下吗
  • 以上代码更改需要在 IdentityServer 中完成。每当 ClientCredential 流生成的令牌时,它将来 ICustomTokenRequestValidator 实现 ValidateAsync 方法,并使用 CustomTokenRequestValidationContext 您可以添加自定义声明。在客户端应用程序/webapi 中,您可以从 JwtToken 中提取声明
猜你喜欢
  • 1970-01-01
  • 2017-11-29
  • 2017-05-14
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 2020-01-15
  • 2021-12-01
  • 2017-10-09
相关资源
最近更新 更多