gpt4 book ai didi

asp.net-core - 如何从 asp.net 核心中的 openid 访问 token 获取声明?

转载 作者:行者123 更新时间:2023-12-01 14:00:16 33 4
gpt4 key购买 nike

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

services.AddAuthentication(o =>
{
o.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(o =>
{
o.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.Scope.Add("openid");
o.Scope.Add("permissions");
o.Authority = "https://localhost:44305";
o.ClientId = "MyTestClient";
o.ClientSecret = "MyTestClientSecret";
o.ResponseType = OpenIdConnectResponseType.IdTokenToken;
});

当我在验证后检查 User 对象时,它只有来自 ID token 的声明,而不是访问 token 。如何从访问 token 中获取声明?

最佳答案

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

services.AddAuthentication(o =>
{
o.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(o =>
{
o.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.Scope.Add("openid");
o.Scope.Add("permissions");
o.Authority = "https://localhost:44305";
o.ClientId = "MyTestClient";
o.ClientSecret = "MyTestClientSecret";
o.ResponseType = OpenIdConnectResponseType.IdTokenToken;
o.Events = new OpenIdConnectEvents
{

OnTokenResponseReceived = ctx =>
{
var handler = new JwtSecurityTokenHandler();
var jsonToken = handler.ReadJwtToken(ctx.TokenEndpointResponse.AccessToken);

//jsonToken.Claims <--here you go, update the ctx.Principal if necessary.


return Task.CompletedTask;
}
};

});

关于asp.net-core - 如何从 asp.net 核心中的 openid 访问 token 获取声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45927299/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com