IdentityServer4.AccessTokenValidation是否执行自动的用户信息自省?

时间:2019-01-04 14:09:19

标签: asp.net-core identityserver4 openid-connect core-authentication

我正在使用身份服务器来保护我的API。 我已经将ApiResource定义为:

return new List<ApiResource> 
        {
            new ApiResource
            {
                Name = "phone.api",
                ApiSecrets={new Secret("secret1".Sha256())},
                Scopes =
                {
                    new Scope()
                    {
                        Name = "phone.api.full_access",
                        DisplayName = "Full access to API"

                    },
                     new Scope
                    {
                        Name = "phone.api.write",
                        DisplayName = "Write and read access to API"
                    },
                    new Scope
                    {
                        Name = "phone.api.read",
                        DisplayName = "Read only access to API"
                    }
                }
                ,UserClaims=new List<string>()
                {
                    "roles",
                   // ClaimTypes.DateOfBirth
                }
            },

还有这样的测试用户:

new TestUser
{
   SubjectId = "1",
   Username = "Billy Admin",
   Password = "password",
   Claims = new List<Claim>()  
   {
      new Claim("roles", "phone.api.admin"),
       new Claim(ClaimTypes.DateOfBirth,new DateTime(1900,10,10).ToString())
   }
 }

该api受以下代码保护:

services.AddAuthentication("Bearer")
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority = Config.AUTHAUTHORITY; //"http://localhost:4000";
                options.ApiName = "phone.api";
                options.ApiSecret = "secret1";
                options.RequireHttpsMetadata = false;
            });

我有一个问题,当一个client + access_token(不包含声明'ClaimTypes.DateOfBirth')时,API将自动调用权限服务器中的用户信息端点以获取额外的声明。 我之所以需要额外的索赔,是因为您在轻视一些政策,例如:

services.AddAuthorization(options =>
{
     options.AddPolicy("Only21", policy => policy.Requirements.Add(new MinimumAgeRequirement(21)));
} );

我的测试向我表明它没有调用userinfo端点,我想知道为什么。我应该用自己的AuthorizationHandler<MinimumAgeRequirement>来称呼它吗?

非常感谢。

1 个答案:

答案 0 :(得分:0)

Badulake的问题很有趣:您如何以最适合该API的方式在OAuth体系结构中管理API声明?

这不仅是OAuth技术问题,还更多是API可扩展性设计问题。我发现在API中管理声明数据效果最好,通常使用声明缓存来使其良好运行。

出于兴趣,这是一个实现上述体系结构的.Net代码示例:

最近我一直在做一些AWSless Serverless API,有趣的是发现AWS API Gateway也使用这种类型的方法。

相关问题