Toni Maccheroni
Toni Maccheroni

Reputation: 61

Microsoft.AspNetCore.Authentication.OpenIdConnect throws "Failed to parse token response body as JSON" error

I'm trying to implement a login to a OpenIdConnect service. To do so, I added the following code to my 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");
            });

I get redirected to the external login page and can enter my credentials. Then, the event "OnAuthenticationFailed" fires with the error:

"Failed to parse token response body as JSON. Status Code: 400. Content-Type: text/html"

After some "Google research", I still have no idea how to fix this issue... This is why I am asking for some help here. Thanks in advance for any kind of hint or tip.

Upvotes: 0

Views: 2881

Answers (1)

Toni Maccheroni
Toni Maccheroni

Reputation: 61

I figured it out. If someone faces the same problem, here is my solution:

The problem was, that on the service provider, the token endpoint authentication mode was set to "client_secret_basic" instead of "client_secret_post". Thus, the token was not saved in the body and therefore the parsing failed. After changing to "client_secret_post", the error was gone and everything worked fine.

Upvotes: 2

Related Questions