2016-08-24 10 views
3

イントロスペクションでOAuthを使用してアクセストークンを検証しています。User.Identityにアクセストークンペイロードの電子メールアドレスを含めるようにする

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": "[email protected]", 
    "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しかし、subemailプロパティが含まれていません。それはちょうど次のようになります。

{ 
    "claims": [ 
     { 
      "issuer": "LOCAL AUTHORITY", 
      "originalIssuer": "LOCAL AUTHORITY", 
      "properties": {}, 
      "subject": { 
       "authenticationType": "Bearer", 
       "isAuthenticated": true, 
       "actor": null, 
       "bootstrapContext": null, 
       "claims": [] 
      } 
     } 
    ] 
} 

我々は特許請求の範囲におけるsubemailプロパティを含めるために私たちのリソースサーバを設定するにはどうすればよいですか?

ここにはour code on GitHubです。

答えて

2

クレームは(彼らがいないのであれば、それは内省ミドルウェアのバグです)がありそうですが、JSON.NETは、これらのタイプは、循環参照を持っているためか、Claim/ClaimsIdentity/ClaimsPrincipalをシリアライズで非常に良いではありません(たとえば、 Claim.Subject/ClaimsIdentity.Claims)。

User.FindFirst(ClaimTypes.NameIdentifier)?.ValueUser.FindFirst(ClaimTypes.Email)?.Value /を使って、件名の識別子とメールアドレスがあることを確認してください。

それが動作する場合は、代わりにClaimsPrincipalインスタンスのご請求の投影を返す考える:

return Json(
    from claim in User.Claims 
    select new { claim.Type, claim.Value } 
); 

ここでは、スクリーンショットのデバッグウィンドウでUserです。

Visual Studio Code Debug Window showing the User

関連する問題