2

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?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Craig
  • 18,074
  • 38
  • 147
  • 248
  • You are telling Swagger to require authentication, but you are not enabling authentication nor configuring it in the ASP.NET Core side – Camilo Terevinto May 02 '18 at 22:48
  • Thanks @CamiloTerevinto - that does seem to be the issue, but I'm not sure where/how to define it. – Craig May 02 '18 at 22:50
  • I won't flag as duplicate (because that would be a lie), but you can find a sample implementation [here](https://stackoverflow.com/questions/45715394/asp-net-core-2-0-bearer-auth-without-identity). As you can see, it's not an easy/fast job. It would be quite good if you implemented that and posted an answer with that + swagger working – Camilo Terevinto May 02 '18 at 22:55

0 Answers0