Swagger UI Auth Error on AuthorizationCode flow

Viewed 1679

I have configured SwaggerUI to use AuthorizationCode flow with LinkedIn. Here is my configuration.

Startup.cs CongfigureServices method:

services.AddSwaggerGen(options =>
{
   options.SwaggerDoc("v1", new OpenApiInfo
   {
      Title = "My Api",
      Version = "v1"
   });

   options.AddSecurityDefinition("OAuth2", new OpenApiSecurityScheme
   {
      Type = SecuritySchemeType.OAuth2,
      Flows = new OpenApiOAuthFlows
      {
         AuthorizationCode = new OpenApiOAuthFlow
         {
            AuthorizationUrl = new Uri(appSettings.Security.Jwt.AuthUrl),
            Scopes = new Dictionary<string, string>
            {
               { "r_liteprofile", ""  },
               { "r_emailaddress", "" }
            },
          }
      }
   });

   options.AddSecurityRequirement(new OpenApiSecurityRequirement
   {
     {
        new OpenApiSecurityScheme
        {
            Reference = new OpenApiReference
            {
                Type = ReferenceType.SecurityScheme,
                Id = "Bearer"
            },
            Scheme = "OAuth2",
            Name = "Authorization",
            In = ParameterLocation.Header
        },
        new string [] {}
    }
  });
});

Configure method in Startup.cs

app
.UseSwagger()
.UseSwaggerUI(option =>
{
    option.SwaggerEndpoint($"/swagger/v1/swagger.json", "My Api v1");
    option.OAuthClientId("myclientid");
    option.OAuth2RedirectUrl("http://localhost:5000/swagger/oauth2-redirect.html");
});

Once, I run the application, I get a swagger UI page with Authorize button. When click on it, I see the following available aurhorizations:

enter image description here

I select the scopes and I'm redirected to linkedin sign in page. When I successfully redirected back with the authorization code, I'm getting:

Auth Error Error: Not Found

Looking at the console I see

swagger-ui-bundle.js:71 POST http://localhost:5000/swagger/index.html 404 (Not Found)

It's trying to make a POST call to http://localhost:5000/swagger/index.html, which gets 404 Not Found.

However, looking at this call, I do see that the authorization code is returned from LinkedIn.

enter image description here

Why is this call keeps failing? I have tried to completely remove the option.Oauth2RedirectUrl and I get the same result. I have also tried setting the redirect url to http://localhost:5000/swagger/index.html, however, when I'm redirected back, it just opens a new tab and no code.

Any suggestion on how I can resolve this?

0 Answers
Related