Skip to content

OAuthCreatingTicketContext still uses Newtonsoft JObject rather than JsonElement as replaced back in 2019? #43034

@WillMarcouiller

Description

@WillMarcouiller

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Currently writing an OAuthHandler. The OAuthCreatingTicketContext still requires a JObject as per below screenshot.

image

Yet, when I look at the source code, it is clear that JObject has been replaced by JsonElement.

https://github.com/dotnet/aspnetcore/blob/0c1a5de1abb14d9b5766374b64c320abb4ac5010/src/Security/Authentication/OAuth/src/Events/OAuthCreatingTicketContext.cs#L15

Also when I look at the file's history, I can see that the change took place on the 5th of Feb. 2019 as per change #7105.

https://github.com/dotnet/aspnetcore/commit/67037a003904efb67d1a9534c0c56936a37d5547#diff-50d7e69d5fb6bc63ce8a4cdd1c89c7de05253ccb7e90b9571946169132a83861

And an announcement has been made for it as well.

https://github.com/dotnet/aspnetcore/issues/7289

Expected Behavior

I then expected to be able to pass on a JsonElement as per examples for Google Authentication and the others.

https://github.com/dotnet/aspnetcore/blob/0c1a5de1abb14d9b5766374b64c320abb4ac5010/src/Security/Authentication/Google/src/GoogleHandler.cs

` ///
protected override async Task CreateTicketAsync(
ClaimsIdentity identity,
AuthenticationProperties properties,
OAuthTokenResponse tokens) {
// Get the Google user
var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);
var response = await Backchannel.SendAsync(request, Context.RequestAborted);
if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException($"An error occurred when retrieving Google user information ({response.StatusCode}). Please check if the authentication information is correct.");
}

        using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync(Context.RequestAborted)))
        {
            // Here, the payload.RootElement is a JsonElement.
            var context = new OAuthCreatingTicketContext(new ClaimsPrincipal(identity), properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
            context.RunClaimActions();
            await Events.CreatingTicket(context);
            return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name);
       }
    }`

Steps To Reproduce

  1. Create new .Net 6 Project
  2. Install package Microsoft.AspNetCore.Authentication.OAuth

image

  1. Replicate code from Google Authentication

https://github.com/dotnet/aspnetcore/blob/0c1a5de1abb14d9b5766374b64c320abb4ac5010/src/Security/Authentication/Google/src/GoogleHandler.cs

  1. Then you should see a red underline for payload.RootElement

image

Exceptions (if any)

image

image

.NET Version

7.0.100-preview.6.22352.1

Anything else?

.Net 6 Project
VS 2022 v17.2.6
Nuget Package Manager v6.2.1

I noticed that the package Microsoft.AspNetCore.Authentication.OAuth was built from an archived source at

https://github.com/aspnet/Security/tree/93926543f8469614c2feb23de8a8c0561b8b2463

Rather than current source like the other authentication providers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions