码迷,mamicode.com
首页 > Web开发 > 详细

asp.net core 3 Swagger 添加 Authorization [Bearer token]

时间:2020-06-19 12:00:46      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:ext   head   sch   tor   ice   rdo   author   desc   for   

// 添加Swagger
            services.AddSwaggerGen(c =>
            {
                //接口文档名称
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "百川新成WEB API", Version = "v1" });
                // 获取xml文件名
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                // 获取xml文件路径
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                // 添加控制器层注释,true表示显示控制器注释
                c.IncludeXmlComments(xmlPath, true);

                //添加Authorization
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "JWT Authorization header using the Bearer scheme.",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Scheme = "bearer",
                    Type = SecuritySchemeType.Http,
                    BearerFormat = "JWT"
                });

                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" }
                        },
                        new List<string>()
                    }
                });
            });

 

asp.net core 3 Swagger 添加 Authorization [Bearer token]

标签:ext   head   sch   tor   ice   rdo   author   desc   for   

原文地址:https://www.cnblogs.com/linyijia/p/13162022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!