【问题标题】:How do I add header documentation in Swashbuckle?如何在 Swashbuckle 中添加标题文档?
【发布时间】:2015-03-04 21:57:09
【问题描述】:

我正在使用库 Swashbuckle。目前没有stackoverflow标签。

我不太明白这里的文档:https://github.com/domaindrivendev/Swashbuckle/blob/master/README.md

标题为“描述安全/授权方案”的部分提到了一段代码

   c.ApiKey("apiKey")
                .Description("API Key Authentication")
                .Name("apiKey")
                .In("header");

但是,当我包含此内容时,什么也没有发生。我也希望它只出现在某些 API 方法上。它确实提到了

"需要与相应的"安全"属性在 文件“

但我不明白。

谁能解释一下?

【问题讨论】:

  • 你试过取消注释吗?
  • 我希望就这么简单 :-) 是的,我确实尝试取消注释

标签: c# swagger swagger-ui swashbuckle


【解决方案1】:

我有同样的问题,并以这种方式解决:

在 SwaggerConfig:

var applyApiKeySecurity = new ApplyApiKeySecurity(
    key: "ServiceBusToken",
    name: "Authorization",
    description: "Service Bus Token, e.g. 'SharedAccessSignature sr=...&sig=...&se=...&skn=...'",
    @in: "header"
);
applyApiKeySecurity.Apply(c);

应用ApiKeySecurity:

public class ApplyApiKeySecurity : IDocumentFilter, IOperationFilter
{
    public ApplyApiKeySecurity(string key, string name, string description, string @in)
    {
        Key = key;
        Name = name;
        Description = description;
        In = @in;
    }

    public string Description { get; private set; }

    public string In { get; private set; }

    public string Key { get; private set; }

    public string Name { get; private set; }

    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, System.Web.Http.Description.IApiExplorer apiExplorer)
    {
        IList<IDictionary<string, IEnumerable<string>>> security = new List<IDictionary<string, IEnumerable<string>>>();
        security.Add(new Dictionary<string, IEnumerable<string>> {
            {Key, new string[0]}
        });

        swaggerDoc.security = security;
    }

    public void Apply(Operation operation, SchemaRegistry schemaRegistry, System.Web.Http.Description.ApiDescription apiDescription)
    {
        operation.parameters = operation.parameters ?? new List<Parameter>();
        operation.parameters.Add(new Parameter
        {
            name = Name,
            description = Description,
            @in = In,
            required = true,
            type = "string"
        });
    }

    public void Apply(Swashbuckle.Application.SwaggerDocsConfig c)
    {
        c.ApiKey(Key)
            .Name(Name)
            .Description(Description)
            .In(In);
        c.DocumentFilter(() => this);
        c.OperationFilter(() => this);
    }
}

那么swagger文件就有了安全定义:

"securityDefinitions":{  
  "ServiceBusToken":{  
     "type":"apiKey",
     "description":"Service Bus Token, e.g. 'SharedAccessSignature sr=...&sig=...&se=...&skn=...'",
     "name":"Authorization",
     "in":"header"
  }
}

应用于文档级别的所有操作:

"security":[  
  {  
     "ServiceBusToken":[]
  }
]

并且所有操作都分配了header参数:

"parameters":[  
   {  
      "name":"Authorization",
      "in":"header",
      "description":"Service Bus Token, e.g. 'SharedAccessSignature sr=...&sig=...&se=...&skn=...'",
      "required":true,
      "type":"string"
   }
]

【讨论】:

    【解决方案2】:

    Swashbuckle 维护者建议我们提供自定义 index.html 来执行此操作,因为他将在下一个主要版本中删除这些配置。看到这个issue

    提供您自己的“索引”文件

    当请求“索引”时,使用 CustomAsset 选项指示 Swashbuckle 返回您的版本而不是默认版本。与所有自定义内容一样,该文件必须作为“嵌入式资源”包含在您的项目中,然后将资源的“逻辑名称”传递给方法,如下所示。有关分步说明,请参阅Injecting Custom Content

    为了兼容性,您应该以this version 为基础自定义“index.html”。

    httpConfiguration
        .EnableSwagger(c => c.SingleApiVersion("v1", "A title for your API"))
        .EnableSwaggerUi(c =>
            {
                c.CustomAsset("index", yourAssembly, "YourWebApiProject.SwaggerExtensions.index.html");
            });
    

    在 index.html 中,您需要将下面的方法更改为如下所示:

    function addApiKeyAuthorization(){
        var key = encodeURIComponent($('#input_apiKey')[0].value);
        if(key && key.trim() != "") {
            var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("sessionId", key, "header");
            window.swaggerUi.api.clientAuthorizations.add("sessionId", apiKeyAuth);
            log("added key " + key);
        }
    }
    

    【讨论】:

    • 与我配置其他所有内容的方式相比,这感觉就像是黑客攻击
    • 当然。不漂亮。我更愿意使用您在问题上向我们展示的配置,但维护者会将其删除... :-( 而且由于错误,它也无法正常工作。
    • 使用 InjectJavaScript 应该是更好的解决方案。请参阅此问题线程:github.com/domaindrivendev/Swashbuckle/issues/222 我使用这种方法在 http 标头中添加不记名令牌以进行授权。
    • 酷!这样我们就不需要替换整个索引文件。谢谢!
    【解决方案3】:
            config.EnableSwagger(c =>
            {
                c.SingleApiVersion("v1", "TestApiWithToken");
    
                c.ApiKey("Token")
                .Description("Filling bearer token here")
                .Name("Authorization")
                .In("header");
            })
            .EnableSwaggerUi(c =>
            {
                c.EnableApiKeySupport("Authorization", "header");
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 2020-04-07
      相关资源
      最近更新 更多