.NET Core 3.0 webapi集成Swagger 5.0
生活随笔
收集整理的這篇文章主要介紹了
.NET Core 3.0 webapi集成Swagger 5.0
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在項目中引用Swashbuckle.AspNetCore和Swashbuckle.AspNetCore.Filters兩個dll,在Startup中的ConfigureServices相關配置代碼如下 ? ? ?
?兩個重點:
1、options.DocumentFilter<HiddenApiFilter>();定義那些接口方法被隱藏
2、啟用oauth2安全授權訪問api接口
options.OperationFilter<SecurityRequirementsOperationFilter>();//給api添加token令牌證書
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Description = "JWT授權(數據將在請求頭中進行傳輸) 直接在下框中輸入Bearer {token}(注意兩者之間是一個空格)\"",
Name = "Authorization",//jwt默認的參數名稱
In = ParameterLocation.Header,//jwt默認存放Authorization信息的位置(請求頭中)
Type = SecuritySchemeType.ApiKey
});
其中使用SecurityRequirementsOperationFilter需要在控制器頭部加[Authorization]或則方法頭部加[Authorization],如下:
[Authorize]public class TokenController : ControllerBase
或者
[Authorize("Customer")]public PersonResponse GetPerson([FromBody]PersonRequest personRequest)
這樣在每個接口才會有小鎖出現。
更多介紹請參考https://github.com/domaindrivendev/Swashbuckle.AspNetCore和https://github.com/mattfrear/Swashbuckle.AspNetCore.Filters
原文地址:https://www.cnblogs.com/cqinwn/p/10918206.html
.NET社區新聞,深度好文,歡迎訪問公眾號文章匯總?http://www.csharpkit.com?
總結
以上是生活随笔為你收集整理的.NET Core 3.0 webapi集成Swagger 5.0的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Angular 8正式发布!
- 下一篇: 站点部署,IIS配置优化指南