0

Im trying to add bearer authentication to swagger api, im very poor with this case and im trying to know something (im 14...) the case is that, the ApiKeyScheme is not found for me... there is my code:

c.AddSecurityDefinition("Bearer",
                            new ApiKeyScheme { In = "header",
                            Description = "Please enter into field the word 'Bearer' following by space and JWT", 
                            Name = "Authorization", Type = "apiKey" });
                            c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>> {
                            { "Bearer", Enumerable.Empty<string>() },
                        });

And those are my libraries:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using EdictApi.Models;
using Microsoft.OpenApi.Models;
using System;
using System.Linq;

And im so confused with it, can someone calm explain me this? :)

3milBow
  • 23
  • 8
  • 1
    What is the error? – Jason Aug 29 '19 at 18:19
  • Try to use swashbuckle. It will make generating the swagger json easier. https://learn.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-2.2&tabs=visual-studio – JM123 Aug 29 '19 at 18:25
  • @Jason Cannot find the type name or namespace "ApiKeyScheme" (no using directive or assembly reference?) – 3milBow Aug 29 '19 at 18:27
  • I did it @JM123 also i connected my DB to swagger and do the dotnet ef migration and it works, but now i would to add bearer to my POSTs... – 3milBow Aug 29 '19 at 18:28
  • I'll do my best to help as I dont use the .net core version of swashbuckle If I understand correctly you have calls that require a Token in the header for the authentication. But as its not part of the Method signature it does not see it and does not generate it? – JM123 Aug 29 '19 at 18:47
  • It just look like that: https://imgur.com/a/6Iuf5kK like i need to add something to "using" statement but i dunno what... – 3milBow Aug 29 '19 at 19:08
  • Im trying to add some method to authorize my requests using swagger bearer but i can't find any way to know it from internet, i can't find any tut :P – 3milBow Aug 29 '19 at 19:12
  • See https://github.com/domaindrivendev/Swashbuckle/issues/501 mrugeshparekh said : // NOTE: You must also configure 'EnableApiKeySupport' below in the SwaggerUI section c.ApiKey("apiKey") .Description("API Key Authentication") .Name("Token") .In("header"); – JM123 Sep 03 '19 at 15:46
  • this helped me fix the same issue https://stackoverflow.com/questions/56234504/migrating-to-swashbuckle-aspnetcore-version-5 – MesfinMoges Sep 24 '19 at 17:16

1 Answers1

2
            c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
            {
                Description =
                    "JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 12345abcdef\"",
                Name = "Authorization",
                In = ParameterLocation.Header,
                Type = SecuritySchemeType.ApiKey,
                Scheme = "Bearer"
            });
            c.IncludeXmlComments($@"{AppDomain.CurrentDomain.BaseDirectory}\I.Api.xml");
            c.OperationFilter<T2ParametersOperationFilter>();
            c.OperationFilter<T1ParametersOperationFilter>();
Behzad F94
  • 198
  • 2
  • 5