使用context.RunClaimsAction(user)在asp.net错误中实现OAuth

时间:2019-09-27 08:06:07

标签: asp.net oauth-2.0

我正在尝试在asp.net 3.0中实现oauth,并且我了解到Microsoft.AspNetCore.Authentication API链接here

中对替换Newtonsoft.Json类型所做的更改。

它不接受用户声明的JObject,并且在尝试将JObject作为参数传递给RunClaimActions(user)时引发错误,并且在运行方法而不传递用户JObject参数时也不会引发错误在方法中。结果是它不会将用户声明返回到视图。有人遇到过这个问题吗?有没有一种方法可以将newtonsoft JObject转换为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);



                    }


                };
            });

1 个答案:

答案 0 :(得分:1)

经过阅读后,我通过使用JsonDocument解析声明并将根文档传递给RunClaimActions方法来解决了这个问题。

    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);



                    }