【问题标题】:Is it possible for Swagger to get the authorization token from URL query string?Swagger 是否可以从 URL 查询字符串中获取授权令牌?
【发布时间】:2020-04-06 04:21:01
【问题描述】:

在一个 .NET Core 项目中,我添加了一个安全定义(底部的代码),它向页面添加了一个授权按钮,用户可以输入 api 密钥 - 一切正常。

是否可以在 URL 中指定 api 键,以便 Swagger 自动使用它而不必输入它?比如/swagger/index.html?authorization=0123456789 或者类似的东西。

现有代码:

services.AddSwaggerGen(c => {
  ...
  c.AddSecurityDefinition("api key", new ApiKeyScheme() {
      Description = "Authorization query string expects API key",
      In = "query",
      Name = "authorization",
      Type = "apiKey"
  });

  var requirements = new Dictionary<string, IEnumerable<string>> {
      { "api key", new List<string>().AsEnumerable() }
  };
  c.AddSecurityRequirement(requirements);
});

似乎带有 authorization 参数的 URL 应该可以工作,但它没有。

附:使用 Swashbuckle 4.0.x

【问题讨论】:

    标签: c# .net-core swagger swashbuckle


    【解决方案1】:

    确实有可能,但您必须覆盖 Swagger-UI 的索引页面,以便将自定义处理程序插入到 onComplete 回调中。

    1. Swashbuckle's source repo 获取最新的 index.html(最好是获取匹配的版本)
    2. 调整 configObject 以添加 OnComplete 回调处理程序,以便在 UI 准备就绪时调用 preauthorizeApiKey
    3. UserSwaggerUI 扩展方法中覆盖 IndexStream 以提供自定义 html

    我最终得到了以下设置(为简洁起见,省略了一些位):

    wwwroot/swashbuckle.html

    <!-- your standard HTML here, nothing special -->
    <script>
        // some boilerplate initialisation
        // Begin Swagger UI call region
        configObject.onComplete = () => {
    
            // get the authorization portion of the query string
            var urlParams = new URLSearchParams(window.location.search);
            if (urlParams.has('authorization')) {
                var apikey = urlParams.get('authorization');
    
                // this is the important bit, see documentation
                ui.preauthorizeApiKey('api key', apikey );// key name must match the one you defined in AddSecurityDefinition method in Startup.cs
           }
        }
        const ui = SwaggerUIBundle(configObject);
        window.ui = ui        
    }
    </script>
    

    Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
            .........
            services.AddSwaggerGen(c => {
                c.SwaggerDoc("v1", new Info { Title = "You api title", Version = "v1" });
                c.AddSecurityDefinition("api key", new ApiKeyScheme() // key name must match the one you supply to preauthorizeApiKey call in JS
                {
                    Description = "Authorization query string expects API key",
                    In = "query",
                    Name = "authorization",
                    Type = "apiKey"
                });
    
                var requirements = new Dictionary<string, IEnumerable<string>> {
                    { "api key", new List<string>().AsEnumerable() }
                };
                c.AddSecurityRequirement(requirements);
            });
        }
    
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.IndexStream = () => File.OpenRead("wwwroot/swashbuckle.html"); // this is the important bit. see documentation https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/README.md
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); // very standard Swashbuckle init
            });
            app.UseMvc();
        }
    

    完成所有这些后,使用 ?authorization=1234567890 调用标准的 swagger URL 应该会自动授权页面。

    【讨论】:

    • 谢谢 - 成功了。我还编辑了您的解决方案以获取 url 参数。
    猜你喜欢
    • 1970-01-01
    • 2017-04-22
    • 2017-11-22
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    相关资源
    最近更新 更多