添加配置
builder.Services.AddSwaggerGen(s => { //添加安全定义 s.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Description = "请输入token,格式为 Bearer xxxxxxxx(注意中间必须有空格)", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey, BearerFormat = "JWT", Scheme = "Bearer" }); //添加安全要求 s.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme{ Reference =new OpenApiReference{ Type = ReferenceType.SecurityScheme, Id ="Bearer" } },new string[]{ } } }); });
测试
参考:.Net6+webApi 给swagger添加tokennet6 webapi openapisecurityscheme菜鸟爱飞不飞的博客-CSDN博客