我正在使用身份服务器来保护我的 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;
});
我有一个问题,当客户端+ access_token (不包含声明 'ClaimTypes.DateOfBirth' 时,API 将自动调用授权服务器中的用户信息端点以获取需要的额外声明。我需要额外声明的原因是检查一些政策,例如:
services.AddAuthorization(options =>
{
options.AddPolicy("Only21", policy => policy.Requirements.Add(new MinimumAgeRequirement(21)));
} );
我的测试表明它没有调用 userinfo 端点,我想知道为什么。我应该自己称呼它AuthorizationHandler<MinimumAgeRequirement>
吗?
非常感谢。