开源DDD设计模式框架YMNNetCoreFrameWork第五篇-Swagger增加权限认证

大城市里の小女人 提交于 2020-02-06 23:44:34
配置文件services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v0.1.0",//版本号
                    Title = "尹大师框架说明,QQ:1390788386",//文档标题
                    Description = "框架说明文档",//文档描述
                    Contact = new OpenApiContact { Name = "道法自然", Email = "1390788386@qq.com"}//联系人
                });
                // Assign scope requirements to operations based on AuthorizeAttribute
                //options.OperationFilter<SecurityRequirementsOperationFilter>();
                //设置swagger的xml文档

                //c.DocInclusionPredicate((docName, description) => true);

                //// Define the BearerAuth scheme that's in use
                //c.AddSecurityDefinition("bearerAuth", new ApiKeyScheme()
                //{
                //    Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                //    Name = "Authorization",
                //    In = "header",
                //    Type = "apiKey"
                //});
                //// Assign scope requirements to operations based on AuthorizeAttribute
                //c.OperationFilter<SecurityRequirementsOperationFilter>();

               // c.DocInclusionPredicate((docName, description) => true);
                //c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                //{
                //    Description = "Authorization format : Bearer {token}",
                //    Name = "Authorization",
                //    In = "header",
                //    Type = "apiKey"
                //});//api界面新增authorize按钮
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "西方输入Token,使用Bearer开头",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey,
                    BearerFormat = "JWT",
                    Scheme = "Bearer"
                });
                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                 {
                   {
                        new OpenApiSecurityScheme
                        {
                       Reference = new OpenApiReference {
                         Type = ReferenceType.SecurityScheme,
                         Id = "Bearer"
                     }
                     },
            new string[] { }
                  }
                  });


                string filepath = $"{AppContext.BaseDirectory}YMNNetCoreFrameWork.Host.xml";
                c.IncludeXmlComments(filepath);
            });

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!