builder.services.AddSwaggerGen(swaggerOptions => { // 配置文档信息 swaggerOptions.SwaggerDoc("v1", new OpenApiInfo() { Title = "WowToolAPI", Contact = new OpenApiContact() { Name = "galens", Url = new Uri("https://galens.uamazing.cn"), Email = "[email protected]" } });
// Set the comments path for the Swagger JSON and UI. // 需要打开生成API文档功能 var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); swaggerOptions.IncludeXmlComments(xmlPath);
// Bearer 的scheme定义 var securityScheme = new OpenApiSecurityScheme() { Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"", Name = "Authorization", //参数添加在头部 In = ParameterLocation.Header, //使用Authorize头部 Type = SecuritySchemeType.Http, //内容为以 bearer开头 Scheme = "bearer", BearerFormat = "JWT" };
//把所有方法配置为增加bearer头部信息 var securityRequirement = new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "bearerAuth" } }, Array.Empty<string>() } };