Asp.net core 如何从asp.net core中的openid访问令牌获取声明?

Asp.net core 如何从asp.net core中的openid访问令牌获取声明?,asp.net-core,openid,asp.net-core-2.0,Asp.net Core,Openid,Asp.net Core 2.0,我的应用程序使用OpenId进行身份验证,如下所示: services.AddAuthentication(o=> { o、 DefaultSignenscheme=CookieAuthenticationDefaults.AuthenticationScheme; o、 DefaultAuthenticateScheme=CookieAuthenticationDefaults.AuthenticationScheme; o、 DefaultChallengeScheme=OpenIdConn

我的应用程序使用OpenId进行身份验证,如下所示:

services.AddAuthentication(o=>
{
o、 DefaultSignenscheme=CookieAuthenticationDefaults.AuthenticationScheme;
o、 DefaultAuthenticateScheme=CookieAuthenticationDefaults.AuthenticationScheme;
o、 DefaultChallengeScheme=OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(o=>
{
o、 SignInScheme=CookieAuthenticationDefaults.AuthenticationScheme;
o、 范围。添加(“openid”);
o、 范围。添加(“权限”);
o、 权威=”https://localhost:44305";
o、 ClientId=“MyTestClient”;
o、 ClientSecret=“MyTestClientSecret”;
o、 ResponseType=OpenIdConnectResponseType.IdTokenToken;
});

当我在身份验证后检查用户对象时,它只有来自ID令牌的声明,而不是来自访问令牌的声明。如何从访问令牌获取声明?

我认为您需要从AddOpenIdConnect()截获OnAuthorizationCodeReceived事件。从那里,您应该可以访问ctx.ProtocolMessage.Code,这是与AcquireTokenByAuthorizationCodeAsync()一起使用的授权代码,用于生成进一步的令牌。您还需要将ResponseType设置为“code id\u token”,以便为您生成代码。这方面的一个很好的教程是。希望这有帮助

您可以使用OpenIdConnectOptions.Events中的OnTokenResponseReceived事件

services.AddAuthentication(o=>
{
o、 DefaultSignenscheme=CookieAuthenticationDefaults.AuthenticationScheme;
o、 DefaultAuthenticateScheme=CookieAuthenticationDefaults.AuthenticationScheme;
o、 DefaultChallengeScheme=OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(o=>
{
o、 SignInScheme=CookieAuthenticationDefaults.AuthenticationScheme;
o、 范围。添加(“openid”);
o、 范围。添加(“权限”);
o、 权威=”https://localhost:44305";
o、 ClientId=“MyTestClient”;
o、 ClientSecret=“MyTestClientSecret”;
o、 ResponseType=OpenIdConnectResponseType.IdTokenToken;
o、 事件=新的OpenIdConnectEvents
{
OnTokenResponseReceived=ctx=>
{
var handler=新的JwtSecurityTokenHandler();
var jsonToken=handler.ReadJwtToken(ctx.TokenEndpointResponse.AccessToken);

//声称我有完全相同的问题,你有没有找到解决办法?Thanks@MatthewChristianson否:(我添加了一个对我有用的答案。我永远无法从OpenIdI获得访问令牌。请注意,这仅在OIDC服务器返回JWT访问令牌时有效。OIDC不要求使用JWT,因为它们也可以是“参考”(或“不透明”)无法解析以获取索赔的令牌。