【问题标题】:Restrict access to certain API controllers in Swagger using Swashbuckle and ASP.NET Identity使用 Swashbuckle 和 ASP.NET Identity 限制对 Swagger 中某些 API 控制器的访问
【发布时间】:2016-01-15 13:13:55
【问题描述】:

所以,我开始使用 Swagger。 我非常喜欢它的功能,但我对所有公开方法的可用性有些怀疑。

据我所知——Swaschbuclke“auth”方法中包含的所有内容实际上都是关于 API 本身的,但我不需要帮助——我的所有 API 都受 API id/key 对保护。

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

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

感谢任何帮助。

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

【问题讨论】:

    标签: asp.net asp.net-identity swagger swagger-ui swashbuckle


    【解决方案1】:

    SwaggerAccessMessageHandler.cs 类添加到您的项目中。

    SwaggerAccessMessageHandler.cs:

    public class SwaggerAccessMessageHandler : DelegatingHandler
    {
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            if (IsSwagger(request))
            {
                if (!Thread.CurrentPrincipal.Identity.IsAuthenticated)
                {
                    // Unauthorized access to swagger 
                    // do any things like : return Unauthorized or NotFound
                    var response = request.CreateResponse(HttpStatusCode.Unauthorized);
                    return Task.FromResult(response);
    
                }
            }
    
            return base.SendAsync(request, cancellationToken);
        }
    
        private bool IsSwagger(HttpRequestMessage request)
        {
             return request.RequestUri.PathAndQuery.StartsWith("/swagger");
        }
    }
    

    在启用 Swagger 之前,在您的 SwaggeConfig.cs (App_start>SwaggeConfig.cs) 中添加处理程序:

    public class SwaggerConfig
    {
        public static void Register()
        {
            // Add here, before EnableSwagger
            GlobalConfiguration.Configuration.MessageHandlers.Add(new SwaggerAccessMessageHandler());
    
            GlobalConfiguration.Configuration
                .EnableSwagger(/*c => ....*/)
                .EnableSwaggerUi(/*c => ....*/);
    
        }
    }
    

    最好的尊重。

    【讨论】:

      【解决方案2】:

      在项目根目录中创建了名为“swagger”的新文件夹。文件夹名称应与 swagger 文档的 url 匹配。

      在新创建的文件夹中添加了新的 Web.config 文件。

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

      找到答案here

      另一种选择是:

      “在我的脑海中,我会说 DelegatingHandler 是你需要的。”

      找到答案here

      【讨论】:

        【解决方案3】:

        关于在 Swagger 文档中限制单个 API 的公开:

        Swashbuckle 5.x:

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

        示例:配置

        httpConfiguration
            .EnableSwagger(c =>
                {
                    c.IgnoreObsoleteActions();
                });
        

        更多信息请访问documentation

        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
            {
               ...
            }
        }
        

        示例:控制器类

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

        GitHub Issue 中的更多详细信息。我自己在 Swashbuckle 4.1.x 中使用过这个。

        【讨论】:

        • 你好。我非常感谢您的回复。但是,不幸的是,[ApiExplorerSettings(IgnoreApi = true)] 对所有用户隐藏了 api,而在实际站点(和 AspUser)上没有他的身份/角色/身份验证状态
        • 感谢您的回答!效果很好。
        • 效果很好 - 很好地隐藏了一些我通常为小型测试创建的端点。
        猜你喜欢
        • 1970-01-01
        • 2017-12-23
        • 2023-04-09
        • 2016-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-06
        相关资源
        最近更新 更多