浏览器刷新或计算机重启后,永久存储Swagger UI令牌 [英] Store Swagger UI Tokens Permanently after Browser Refresh or Computer Restart

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

问题描述

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

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

我们正在测试Net Core Apis,并通过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 Environment注入代码,而不会在源代码控件中公开令牌?

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,

仅在质量检查和生产阶段需要输入令牌

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令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆