Reading JSON response in OnCreatingTicket without using MapJsonKey (claims)

1k views Asked by At

I am trying to read the actual JSON response from Google API after successful OAuth authentication flow. I have read the MS Docs (https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social/additional-claims?view=aspnetcore-5.0#establish-the-authentication-scope) where they use options.ClaimActions.MapJsonKey to map each key to a Claim. However I do not want to add claims, I just want to read all of the JSON keys and values returned.

.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;
    };
1

There are 1 answers

0
user2768479 On BEST ANSWER

Instead of checking the Context.Response for the JSON payload, one has to access the Context.TokenResponse, the "response from an provider for an OAuth token request":

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"

Source: https://learn.microsoft.com/en-nz/dotnet/api/microsoft.aspnetcore.authentication.oauth.oauthtokenresponse?view=aspnetcore-2.0.