1. 程式人生 > >Authorize requests in Swagger UI

Authorize requests in Swagger UI

idt .html swagger rop app -s div In security

Authorize requests in Swagger UI

前面的文章講到了Swagger+ASP.NET Core的使用,這裏講怎麽把授權認證加入Swagger中。

國外的一篇文章:https://ppolyzos.com/2017/10/30/add-jwt-bearer-authorization-to-swagger-and-asp-net-core/

 app.UseSwaggerUi3(typeof(Startup).GetTypeInfo().Assembly, settings =>
            {
                settings.GeneratorSettings.DefaultPropertyNameHandling 
= PropertyNameHandling.CamelCase; settings.PostProcess = document => { document.Info.Version = "v1"; document.Info.Title = "ApiGateWay"; document.Info.Description = "移動平臺Api網關"; document.SecurityDefinitions.Add(
"Bearer", new NSwag.SwaggerSecurityScheme { Description = "JWT Authorization header using the Bearer scheme. Example: \"Bearer {token}\"", Name = "Authorization", Type = NSwag.SwaggerSecuritySchemeType.ApiKey, In
= NSwag.SwaggerSecurityApiKeyLocation.Header }); document.Security.Add(new NSwag.SwaggerSecurityRequirement { { "Bearer", new string[] { } }, }); }; });

技術分享圖片

Authorize requests in Swagger UI