using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; using System; using System.Reflection; using System.Linq; using Microsoft.Extensions.DependencyInjection; namespace XYY.Core.SwaggerGen.Strandard { public static class StrandardApiAuth { public static void SetAuth(this SwaggerGenOptions c) { var apiSS = new OpenApiSecurityScheme() { Description = "请求头必须添加token,传入token: \"Authorization: token {token}\"", Name = "Authorization", //参数添加在头部 In = ParameterLocation.Header, //使用Authorize头部 Type = SecuritySchemeType.Http, //内容为以 bearer开头 Scheme = "bearer", }; var securityRequirement = new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "bearerAuth", } }, new string[] {} } }; c.AddSecurityDefinition("bearerAuth", apiSS); c.AddSecurityRequirement(securityRequirement); } } }