Swashbuckle and OAuth Client Credentials

Viewed 312

I am using Swashbuckle.AspNet.Core 6.23 with .NET 6; generating an OpenAPI file and viewing it using SwaggerUI. My API uses the client credential flow. When attempting to authenticate in the Swagger UI I get a "404 not found" when the Swagger UI attempts to post back to Swagger UIs index.html. My expectation was that it would redirect to the OAuth login page, but that is not what is happening. Not sure what I am getting wrong here.

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {Title = "Digital Room Shipping Rates API", Version = "v1"});
                c.SchemaFilter<RequireGroupFilter>();
                c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "DigitalRoom.ShippingRatesAPI.xml"));
                c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "DigitalRoom.ShippingCarriers.xml"));
                c.EnableAnnotations(true, false);
                
                c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
                {
                    Type = SecuritySchemeType.OAuth2,
                    Flows = new OpenApiOAuthFlows()
                    {
                        ClientCredentials = new OpenApiOAuthFlow()
                        {
                            AuthorizationUrl = new Uri("https://[myoauthserver]/oauth")
                        }
                    }
                });                
            });
         }


// and in my Configure...

            app.UseHttpsRedirection();
            app.UseExceptionHandler("/error");
            app.UseAuthentication();

            app.UseRouting();
            app.UseAuthorization();

                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    // Tried with and without having this commented out...
                    // c.OAuthClientId("abc");
                    // c.OAuthClientSecret("def");
                    // c.OAuthAppName("swagger");
                    // c.OAuthScopeSeparator(" ");
                    // c.OAuth2RedirectUrl("https://[myoauthserver]/oauth");
                    // c.OAuthScopes("test-api");
                    // c.OAuthUseBasicAuthenticationWithAccessCodeGrant();
                });
            }

            app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
        }
0 Answers
Related