本文介绍了在浏览器刷新或计算机重新启动后永久存储 Swagger UI 令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there any method to hardcode tokens into Swagger UI permanently in Local computer or Dev Environment ?

我们正在测试 Net Core API 并通过 Angular 8 运行它们:每天开发、重建、编写代码、测试 Swagger API 超过 100 次.寻找方法,将令牌存储在下面,因此不必继续重新输入.随着分钟数的增加,可能会非常麻烦、耗时.

We are testing Net Core Apis and also running them through Angular 8: developing, rebuilding, writing code, testing Swagger APIs over 100 times a day each.Looking for way, to store token below, so don't have to keep Reentering.Can be very cumbersome, consuming time, as minutes add up.

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

Maybe we can read a token from external file in developer desktop. Token stays so even after computer restarts, developers are not required to reenter tokens. Perhaps in appsettings.json or any file?

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

Or anyway to inject code with Net Core Visual Studio Environment, that does not expose token in Source control?

答案应该运行在 Angular 环境中运行的所有 Swagger UI 和 API,

Answer should run all Swagger UI and APIs ran from Angular environment,

仅在 QA 和 Production 中需要输入令牌

Only in QA and Production will require entry of token

使用 Angular 和 Net Core 2.0 C#,

Using Angular and Net Core 2.0 C#,

推荐答案

调整我的 其他答案 对于您的情况,您的设置可能如下所示:

Adapting my other answer to your case, your setup can look like follows:

    <!-- 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();
        }

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

There are different ways to deliver your key to swagger, hard-coding might be not the best, but it should hopefully get you started.Since you indicate you only want this functionality for development environment I opted to only serve the modified file if (env.IsDevelopment()), which you, again, can tweak to your needs

这篇关于在浏览器刷新或计算机重新启动后永久存储 Swagger UI 令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 23:06