我们通过自省使用 OAuth 来验证访问令牌。
app.UseOAuthIntrospection(options =>
{
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;
options.Authority = "http://localhost:12345/";
options.Audiences.Add("ResourceServer01");
options.ClientId = "ResourceServer01";
options.ClientSecret = "secret_secret_secret";
});
这主要是有效的。
授权服务器响应connect/introspect
良好。
{
"active": true,
"iss": "http://localhost:12345/",
"sub": "797264b3-194c-483f-08fb-08d3cbab9158",
"scope": "openid email roles",
"iat": 1471998289,
"nbf": 1471998289,
"exp": 1472000089,
"jti": "274cbb7f-9412-4d69-8c02-ca6a500b4a36",
"token_type": "Bearer",
"aud": [
"ResourceServer01",
"ResourceServer02"
],
"email": "shaun@bigfont.ca",
"AspNet.Identity.SecurityStamp": "4956a5c3-9efd-4f51-9746-43a187698e1e"
}
对资源服务器的请求通过了该Authorize
属性。这也很好。
[Authorize(ActiveAuthenticationSchemes = OAuthValidationDefaults.AuthenticationScheme)]
[HttpGet("message")]
public IActionResult GetMessage() {
var identity = User.Identity as ClaimsIdentity;
if (identity == null) {
return BadRequest();
}
return Json(User);
}
虽然User
,不包含sub
也不包含email
属性。它看起来像这样:
{
"claims": [
{
"issuer": "LOCAL AUTHORITY",
"originalIssuer": "LOCAL AUTHORITY",
"properties": {},
"subject": {
"authenticationType": "Bearer",
"isAuthenticated": true,
"actor": null,
"bootstrapContext": null,
"claims": []
}
}
]
}
我们如何配置我们的资源服务器以在声明中包含sub
和email
属性?