【问题标题】:Store Swagger UI Tokens Permanently after Browser Refresh or Computer Restart在浏览器刷新或计算机重新启动后永久存储 Swagger UI 令牌
【发布时间】:2020-04-21 14:19:12
【问题描述】:

是否有任何方法可以在本地计算机或开发环境中将令牌永久硬编码到 Swagger UI 中?

我们正在测试 Net Core API 并通过 Angular 8 运行它们:开发、重建、编写代码、每天测试 Swagger API 超过 100 次。 找方法,把token存放在下面,不用一直Reentering。 随着分钟数的增加,可能会非常麻烦、耗时。

也许我们可以从开发人员桌面的外部文件中读取令牌。即使在计算机重新启动后,令牌仍然存在,开发人员无需重新输入令牌。也许在 appsettings.json 或任何文件中?

或者无论如何使用 Net Core Visual Studio 环境注入代码,不会在源代码管理中公开令牌?

Answer 应该运行从 Angular 环境运行的所有 Swagger UI 和 API,

仅在 QA 和生产中需要输入令牌

使用 Angular 和 Net Core 2.0 C#,

【问题讨论】:

  • 您可以使用基于浏览器的自动化工具(如 Selenium)插入令牌,而不是在代码中执行此操作。
  • 好吧,我们没有使用 Selenium,开发人员正在手动重建和编写代码,在他们的桌面上重新运行 api,@shobhonk
  • 对于我在开发 .net core 或 asp .net 服务时所做的工作,我使用 postman 应用程序。我将令牌作为全局变量并从那里进行测试。它节省了我每次大摇大摆地输入令牌的时间,如果您要每天测试 100 多次,我认为这是不必要的。 Postman 还会保留令牌,直到您需要更改它。如果需要,您还可以自动化该过程
  • 如果邮递员在重新启动计算机后保存令牌,那将有所帮助,同时也在寻找 swagger 解决方案,我们的客户客户无论出于何种原因都更喜欢 swagger,
  • 是邮递员持久保存或更新的变量。我也明白要求客户使用邮递员可能对客户来说是一个很大的要求,如果他们还没有使用它。但它是工程世界尽可能使用阻力最小的路径,而不是试图找到可能成为问题的解决方案,而其他人可能不知道(支持或其他)。但这只是我的意见。我会四处挖掘,看看代码是否可行

标签: c# angular asp.net-core .net-core swagger


【解决方案1】:

根据您的情况调整我的other answer,您的设置可能如下所示:

wwwroot/swashbuckle.html

    <!-- your standard HTML here, nothing special -->
    <script>
        // some boilerplate initialisation
        // Begin Swagger UI call region
        configObject.onComplete = () => {
                // this is the important bit, see documentation
                ui.preauthorizeApiKey('api key', 'HARDCODE YOUR KEY HERE' );// 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 =>
            {
                if (env.IsDevelopment()) // override swashbuckle index page only if in development env
                {
                    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();
        }

有多种方法可以将您的密钥传递给 Swagger,硬编码可能不是最好的,但它应该可以帮助您入门。 由于您表示您只希望此功能用于开发环境,因此我选择仅提供修改后的文件 if (env.IsDevelopment()),您可以再次根据您的需要进行调整

【讨论】:

  • 给出了分数并接受了答案,请随意竖起大拇指
  • 快速提问,将其添加到项目中的工作量一般是多少?我需要给经理估价,5 小时或更少? 10个小时?认为最多需要 2-3 个小时
  • 一旦你知道把它放在哪里就相当简单了,不是吗?
  • 这是一个很好的答案,而不是更改源代码,是否有 javascript 方法可以在浏览器 chrome 控制台中保存令牌?
  • 不确定我是否理解问题陈述。所以你只是想打印出来?如果您想动态传递它,请查看我参考的答案:它有 URL 参数的代码
【解决方案2】:

【讨论】:

【解决方案3】:

我设法在 ASP.NET Core 5 中通过将此行添加到 startup.cs, Configure 方法来做到这一点

        app.UseSwaggerUI(c =>
        {
            c.ConfigObject.AdditionalItems.Add("persistAuthorization","true");
        });

我通过阅读docs 找到了这个 还有here

【讨论】:

    猜你喜欢
    • 2012-07-03
    • 1970-01-01
    • 2019-03-24
    • 2020-11-05
    • 2020-01-22
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    相关资源
    最近更新 更多