c# - 在 OnCreatingTicket 中读取 JSON 响应而不使用 MapJsonKey(声明)
问题描述
在 OAuth 身份验证流程成功后,我正在尝试从 Google API 读取实际的 JSON 响应。我已阅读 MS Docs ( 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;
};
解决方案
不必检查 JSON 有效负载的Context.Response ,而是必须访问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"
来源:https ://docs.microsoft.com/en-nz/dotnet/api/microsoft.aspnetcore.authentication.oauth.oauthtokenresponse?view=aspnetcore-2.0 。
推荐阅读
- rust - 使用 serde 序列化嵌套的对象数组
- javascript - 如何从 JavaScript 的下拉菜单中查询数值?
- python - 旋转基于其他字符串的字符串
- ios - UIScrollView 中的子视图控制器
- android - 我无法将颤振下载到 vs 代码。如何手动安装 VSIX?
- node.js - 如何在 NodeJS 中使用 Array 输入重复相同的 Promise,直到它解析或 Array 结束?
- javascript - 文件上传进度条干扰控制器重定向
- r - 带有附加列的 R 频率表
- ruby-on-rails - 将 axlsx gem 与 conditional_formatting 一起使用
- pandas - 在列透视期间保留最后一个重复索引?