Microsoft.AspNetCore.Authentication.OpenIdConnect 抛出 "Failed to parse token response body as JSON" 错误
Microsoft.AspNetCore.Authentication.OpenIdConnect throws "Failed to parse token response body as JSON" error
我正在尝试实现对 OpenIdConnect 服务的登录。为此,我将以下代码添加到我的 Startup.cs:
services.AddAuthentication(options => {
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options => {
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.Authority = identityUrl.ToString();
options.SignedOutRedirectUri = callBackUrl.ToString();
options.ClientId = "xxx";
options.ClientSecret = "yyy";
options.AuthenticationMethod = OpenIdConnectRedirectBehavior.RedirectGet;
options.ResponseType = OpenIdConnectResponseType.Code;
options.ResponseMode = OpenIdConnectResponseMode.Query;
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.CorrelationCookie = new Microsoft.AspNetCore.Http.CookieBuilder();
options.Events = new OpenIdConnectEvents {
OnAuthenticationFailed = context => {
context.HandleResponse();
context.Response.Redirect("/");
return Task.CompletedTask;
}
};
options.Scope.Add("openid");
});
我被重定向到外部登录页面并可以输入我的凭据。然后,事件“OnAuthenticationFailed”触发并出现错误:
“无法将令牌响应正文解析为 JSON。状态代码:400。内容类型:text/html”
经过一些“Google 研究”,我仍然不知道如何解决这个问题...这就是我在这里寻求帮助的原因。预先感谢任何类型的提示或技巧。
我明白了。如果有人遇到同样的问题,这是我的解决方案:
问题是,在服务提供商上,令牌端点身份验证模式设置为“client_secret_basic”而不是“client_secret_post”。因此,令牌未保存在正文中,因此解析失败。更改为“client_secret_post”后,错误消失,一切正常。
我正在尝试实现对 OpenIdConnect 服务的登录。为此,我将以下代码添加到我的 Startup.cs:
services.AddAuthentication(options => {
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options => {
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.Authority = identityUrl.ToString();
options.SignedOutRedirectUri = callBackUrl.ToString();
options.ClientId = "xxx";
options.ClientSecret = "yyy";
options.AuthenticationMethod = OpenIdConnectRedirectBehavior.RedirectGet;
options.ResponseType = OpenIdConnectResponseType.Code;
options.ResponseMode = OpenIdConnectResponseMode.Query;
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.CorrelationCookie = new Microsoft.AspNetCore.Http.CookieBuilder();
options.Events = new OpenIdConnectEvents {
OnAuthenticationFailed = context => {
context.HandleResponse();
context.Response.Redirect("/");
return Task.CompletedTask;
}
};
options.Scope.Add("openid");
});
我被重定向到外部登录页面并可以输入我的凭据。然后,事件“OnAuthenticationFailed”触发并出现错误:
“无法将令牌响应正文解析为 JSON。状态代码:400。内容类型:text/html”
经过一些“Google 研究”,我仍然不知道如何解决这个问题...这就是我在这里寻求帮助的原因。预先感谢任何类型的提示或技巧。
我明白了。如果有人遇到同样的问题,这是我的解决方案:
问题是,在服务提供商上,令牌端点身份验证模式设置为“client_secret_basic”而不是“client_secret_post”。因此,令牌未保存在正文中,因此解析失败。更改为“client_secret_post”后,错误消失,一切正常。