1

在 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;
    };
4

1 回答 1

0

不必检查 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 。

于 2021-02-01T14:04:37.710 回答