From 79151e7da8b5452293a18ad178654bb40fb521c1 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Fri, 21 Jun 2024 21:43:40 +0300 Subject: [PATCH] feat: add bearer auth to swagger --- .../AppConfig/SwaggerConfiguration.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Endpoint/Configuration/AppConfig/SwaggerConfiguration.cs b/Endpoint/Configuration/AppConfig/SwaggerConfiguration.cs index c43c730..4c2ff9f 100644 --- a/Endpoint/Configuration/AppConfig/SwaggerConfiguration.cs +++ b/Endpoint/Configuration/AppConfig/SwaggerConfiguration.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; +using Microsoft.OpenApi.Models; using Mirea.Api.Endpoint.Configuration.Swagger; using Swashbuckle.AspNetCore.SwaggerGen; using System; @@ -19,6 +20,29 @@ public static class SwaggerConfiguration options.OperationFilter(); var basePath = AppDomain.CurrentDomain.BaseDirectory; + options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme + { + In = ParameterLocation.Header, + Description = "Keep the JWT token in the field (Bearer token)", + Name = "Authorization", + Type = SecuritySchemeType.ApiKey + }); + + options.AddSecurityRequirement(new OpenApiSecurityRequirement + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference + { + Type = ReferenceType.SecurityScheme, + Id = "Bearer" + } + }, + [] + } + }); + options.IncludeXmlComments(Path.Combine(basePath, "docs.xml")); options.IncludeXmlComments(Path.Combine(basePath, "ApiDtoDocs.xml")); });