zoukankan      html  css  js  c++  java
  • Core3.0 升级swagger 配置区别

    Nuget 安装包区别

    Core2.2

     Core3.0

     

    IDocumentFilter区别

    Core2.2

     

    Core3.0

    Core 2.2

    app.UseSwagger(c =>
    {
      c.PreSerializeFilters.Add((swaggerDoc, httpReq) => swaggerDoc.Host = httpReq.Host.Value);
    });
    

      

    Core 3.0

    app.UseSwagger(c =>
    {
      c.PreSerializeFilters.Add((swagger, httpReq) =>
      {
        swagger.Servers = new List<OpenApiServer> { new OpenApiServer { Url = $"{httpReq.Scheme}://{httpReq.Host.Value}" } };
      });
    });
    

    原使用到的info改变为OpenApiInfo

    Microsoft.OpenApi 代替 Swashbuckle.AspNetCore.Swagger

    Core 2.2

    Core 3.0

    Swagger 添加 token Header区别

    Core 2.2

    public class AddAuthTokenHeaderParameter : IOperationFilter
        {
            public void Apply(Operation operation, OperationFilterContext context)
            {
                if (operation.Parameters == null)
                    operation.Parameters = new List<IParameter>();
    
                var attrs = context.ApiDescription.ActionDescriptor.AttributeRouteInfo;
    
                //先判断是否是匿名访问,
                var descriptor = context.ApiDescription.ActionDescriptor as ControllerActionDescriptor;
                if (descriptor != null)
                {
                    //var isAuthorized = descriptor..Select(filterInfo => filterInfo.Instance).Any(filter => filter is IAuthorizationFilter);
    
                    var actionAttributes = descriptor.MethodInfo.GetCustomAttributes(inherit: true);
    
                    bool isAnonymous = actionAttributes.Any(a => a is AllowAnonymousAttribute);
                    bool isAuthorized = actionAttributes.Any(a => a is AuthorizeAttribute);
                    //非匿名的方法,链接中添加accesstoken值
                    if (!isAnonymous && isAuthorized)
                    {
                        operation.Parameters.Add(new NonBodyParameter()
                        {
                            Name = "Authorization",
                            In = "header",//query header body path formData
                            Type = "string",
                            Default = "",
                            Required = true //是否必选
                        });
                    }
    
                }
            }
        }
    

      Core 3.0

    public class AddAuthTokenHeaderParameter : IOperationFilter
        {
            public void Apply(OpenApiOperation operation, OperationFilterContext context)
            {
                if (operation.Parameters == null)
                {
                    operation.Parameters = new List<OpenApiParameter>();
                }
    
                var attrs = context.ApiDescription.ActionDescriptor.AttributeRouteInfo;
    
                //先判断是否是匿名访问,
                var descriptor = context.ApiDescription.ActionDescriptor as ControllerActionDescriptor;
                if (descriptor != null)
                {
                    //var isAuthorized = descriptor..Select(filterInfo => filterInfo.Instance).Any(filter => filter is IAuthorizationFilter);
    
                    var actionAttributes = descriptor.MethodInfo.GetCustomAttributes(inherit: true);
    
                    bool isAnonymous = actionAttributes.Any(a => a is AllowAnonymousAttribute);
                    bool isAuthorized = actionAttributes.Any(a => a is AuthorizeAttribute);
                    //非匿名的方法,链接中添加accesstoken值
                    if (!isAnonymous && isAuthorized)
                    {
                        operation.Parameters.Add(new OpenApiParameter()
                        {
                            Name = "Authorization",
                            In= ParameterLocation.Header,//query header body path formData
                            Description="Token验证",
                             Schema = new OpenApiSchema() { 
                                Type="string",
                             },
                            //In = "header",//query header body path formData
                            //Type = "string",
                            //Default = "",
                            Required = true //是否必选
                        });
                    }
    
                }
            }
        }
    

      

  • 相关阅读:
    Linux 文件和目录的属性
    关于样式加载顺序,js加载顺序
    关于css切换菜单
    jquery插件编写思路
    京东中关于领券地址的安全处理
    把js写到链接a标签的href中和写到onclick中的区别
    京东中关于jsonp的运用
    关于Jquery中ajax方法data参数用法的总结
    js中的延迟加载
    Jquery版Ajax利用JSONP 跨域POST/GET传输数据研究
  • 原文地址:https://www.cnblogs.com/zxcnn/p/11654261.html
Copyright © 2011-2022 走看看