I'm moving my Asp.Net WebAPI endpoints to a .Net Core 2.0 project, but am battling with Swagger for API testing. At the moment, I am getting the following error:
InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found.
I am running Swashbuckle.AspNetCore 2.4.0
I added my AddSwashbuckle to my ConfigureService in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddSwaggerDocumentation();
services.AddMvc();
}
And in the Configure method, I added the Add line for swashbuckle.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwaggerDocumentation();
}
app.UseCors(builder => builder.WithOrigins("*"));
app.UseMvc();
}
I added the extension method for handling Swagger.
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Swashbuckle.AspNetCore.Swagger;
using System.Collections.Generic;
namespace JwtSwaggerDemo.Infrastructure
{
public static class SwaggerServiceExtensions
{
public static IServiceCollection AddSwaggerDocumentation(this IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1.0", new Info { Title = "Main API v1.0", Version = "v1.0" });
// Swagger 2.+ support
var security = new Dictionary<string, IEnumerable<string>>
{
{"Bearer", new string[] { }},
};
c.AddSecurityDefinition("Bearer", new ApiKeyScheme
{
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
In = "header",
Type = "apiKey"
});
c.AddSecurityRequirement(security);
});
return services;
}
public static IApplicationBuilder UseSwaggerDocumentation(this IApplicationBuilder app)
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1.0/swagger.json", "Versioned API v1.0");
c.DocumentTitle = "Title Documentation";
c.DocExpansion(DocExpansion.None);
});
return app;
}
}
}
(Note this was following this post.
However, I get the error as stated above, and cannot see how I can get this to work. I seem to be missing something somewhere. How can I resolve this issue or see where the problem lies?