如何使用swagger swashbuckle保护生成的API文档

时间:2016-08-29 13:15:10

标签: c# asp.net-web-api swagger swashbuckle

我使用swagger swashbukle实现了API文档。现在我想在我的网站上发布生成的文档作为帮助文件。如何保护此链接并发布?

3 个答案:

答案 0 :(得分:2)

只有在解决方案中实施了身份验证后,此解决方案才有效。

  1. 在解决方案中创建新的文件夹招摇
  2. 添加新的Web.config文件。
  3. 在其中编写以下代码:

    <configuration> 
        <system.web> 
            <authorization> 
                <deny users="?" /> 
            </authorization> 
        </system.web> 
        <system.webServer> 
            <modules runAllManagedModulesForAllRequests="true" /> 
        </system.webServer> 
    </configuration>
    

答案 1 :(得分:1)

到目前为止,我认为没有办法确保swagger swashbuckle端点的安全。您可以从他们的github问题here

获得更多信息

答案 2 :(得分:1)

我想出了这样做的方法。使用最新的swashbuckle版本,并在注入的index.html

中添加以下div标签

<div id ='auth_container'&gt;

这将在swagger UI中显示一个可用于身份验证的授权按钮,一旦经过身份验证,对于API的所有请求,JWT令牌将从swagger UI传递

另外,请按照以下链接保护swagger文档的所有调用 - https://github.com/domaindrivendev/Swashbuckle/issues/601