在不使用 MapJsonKey 的情况下读取 OnCreatingTicket 中的 JSON 响应(声明)
Reading JSON response in OnCreatingTicket without using MapJsonKey (claims)
我正在尝试在 OAuth 身份验证流程成功后读取来自 Google API 的实际 JSON 响应。我已经阅读了 MS 文档 (https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/additional-claims?view=aspnetcore-5.0#establish-the-authentication-scope),他们在其中使用 options.ClaimActions.MapJsonKey 将每个键映射到声明。但是我不想添加声明,我只想阅读返回的所有 JSON 键和值。
.AddGoogle(options => {
options.ClientId = _conf["MyClientId"];
options.ClientSecret = _conf["MySecret"];
options.Events.OnCreatingTicket = ctx => {
// How do I read the JSON returned, via ctx object? I have tried ctx.Response.Body, but didn't get much further.
return Task.CompletedTask;
};
不必检查 Context.Response 中的 JSON 负载,而是必须访问 Context.TokenResponse,“提供商对 OAuth 令牌请求的响应”:
options.Events.OnCreatingTicket = ctx => {
var payload = ctx.TokenResponse.Response;
// Do something with the payload, deserialize or whatever, now you can access all JSON key/value pairs such as "profile_picture" or "some_key"
我正在尝试在 OAuth 身份验证流程成功后读取来自 Google API 的实际 JSON 响应。我已经阅读了 MS 文档 (https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/additional-claims?view=aspnetcore-5.0#establish-the-authentication-scope),他们在其中使用 options.ClaimActions.MapJsonKey 将每个键映射到声明。但是我不想添加声明,我只想阅读返回的所有 JSON 键和值。
.AddGoogle(options => {
options.ClientId = _conf["MyClientId"];
options.ClientSecret = _conf["MySecret"];
options.Events.OnCreatingTicket = ctx => {
// How do I read the JSON returned, via ctx object? I have tried ctx.Response.Body, but didn't get much further.
return Task.CompletedTask;
};
不必检查 Context.Response 中的 JSON 负载,而是必须访问 Context.TokenResponse,“提供商对 OAuth 令牌请求的响应”:
options.Events.OnCreatingTicket = ctx => {
var payload = ctx.TokenResponse.Response;
// Do something with the payload, deserialize or whatever, now you can access all JSON key/value pairs such as "profile_picture" or "some_key"