gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:25:56 30 4
gpt4 key购买 nike

我正在尝试在 asp.net 3.0 中实现 oauth,并且我了解 Microsoft.AspNetCore.Authentication API 链接 here 中替换的 Newtonsoft.Json 类型的更改发生了一些变化。

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



}


};
});

最佳答案

经过一番阅读后,我通过使用 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);



}

关于asp.net - 使用 context.RunClaimsAction(user) 在 asp.net 错误中实现 OAuth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58130357/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com