使用Swashbuckle和ASP.NET Identity限制对Swagger中某些API控制器的访问

Mik*_*rov 10 asp.net swagger asp.net-identity swagger-ui swashbuckle

所以,我开始使用Swagger.我完全爱上它的功能,但我对所有公开方法的可用性有所怀疑.

据我所知 - Swaschbuclke中包含的所有"auth"方法实际上都是关于API本身,但我不需要帮助 - 我的所有API都受API id /密钥对的保护.

我想以某种方式利用ASP.NET身份(登录系统)来限制对API页面的访问(/ swagger/ui/index).

有什么办法吗?Swaschbuckle中的任何方法?任何路线/身份黑客?

任何帮助表示赞赏.

编辑1:[ApiExplorerSettings(IgnoreApi = true)]属性不是我正在寻找的 - 它限制了对方法的所有访问,无论身份如何.

Ant*_*ace 18

关于限制swigger文档中单个API的暴露:

Swashbuckle 5.x:

Swashbuckle 5.x有一个名为IgnoreObsoleteActions的配置选项(您需要设置它;默认情况下不启用它),如果它们具有该[Obsolete]属性,它将隐藏操作.

示例:配置

httpConfiguration
    .EnableSwagger(c =>
        {
            c.IgnoreObsoleteActions();
        });
Run Code Online (Sandbox Code Playgroud)

文档中提供了更多信息.

Swashbuckle 4.1.x(或者如果你不想使用过时的属性):

Swashbuckle在IApiExplorer之上构建了一个swagger文档.您应该能够添加一个属性 - [ApiExplorerSettings(IgnoreApi = true)]用于管理ApiExplorerSettings控制器类或单个控制器方法,以便在生成文档时让资源管理器(以及随后的Swashbuckle)忽略它们.

示例:个人行为

/// Ignore 'GetFoo' in documentation
public class FooBarController
{
    [ApiExplorerSettings(IgnoreApi = true)]
    public Bar GetFoo
    {
       ...
    }

    public Bar GetBar
    {
       ...
    }
}
Run Code Online (Sandbox Code Playgroud)

示例:控制器类

/// Ignore every controller method in FooBarController in documentation
[ApiExplorerSettings(IgnoreApi = true)]
public class FooBarController
{
    public Bar GetFoo
    {
       ...
    }

    public Bar GetBar
    {
       ...
    }
}
Run Code Online (Sandbox Code Playgroud)

这个GitHub问题的更多细节.我自己在Swashbuckle 4.1.x中使用过它.