Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing OAuth in asp.net error with context.RunClaimsAction(user)

I am trying to implement oauth in asp.net 3.0, and I understand there were some changes as regards to changes to the Newtonsoft.Json types replaced in Microsoft.AspNetCore.Authentication APIs link here

It does not accept a JObject of the user's claims and throws an error when you try to pass a JObject as a parameter to the RunClaimActions(user) and does not throw an error when you run the method without passing the user JObject parameter in the method. The result is that it does not return the User claims to the view. Has anyone faced this problem yet ? Is there a way to convert a newtonsoft JObject to a JsonElement.

options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
                options.ClaimActions.MapJsonKey(ClaimTypes.Name, "name");
                options.ClaimActions.MapJsonKey("urn:github:login", "login");
                options.ClaimActions.MapJsonKey("urn:github:url", "html_url");
                options.ClaimActions.MapJsonKey("urn:github:avatar", "avatar_url");

                options.Events = new OAuthEvents
                {
                    OnCreatingTicket = async context =>
                    {
                        var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
                        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);

                        var response = await context.Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, context.HttpContext.RequestAborted);
                        response.EnsureSuccessStatusCode();

                        var user = JObject.Parse(await response.Content.ReadAsStringAsync());
                        context.RunClaimActions(user);



                    }


                };
            });
like image 433
Camillus Chinaedu Teteh Avatar asked Dec 14 '22 10:12

Camillus Chinaedu Teteh


1 Answers

After some reading, I solved the issue by using the JsonDocument for parsing the claims and passing the root document to the RunClaimActions method.

    options.Events = new OAuthEvents
                {
                    OnCreatingTicket = async context =>
                    {
                        var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
                        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);

                        var response = await context.Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, context.HttpContext.RequestAborted);
                        response.EnsureSuccessStatusCode();

                        var user = JsonDocument.Parse(await response.Content.ReadAsStringAsync());                                              
                        context.RunClaimActions(user.RootElement);



                    }
like image 67
Camillus Chinaedu Teteh Avatar answered May 13 '23 14:05

Camillus Chinaedu Teteh