如何重置EntityFramework .NET Core WebServer的ConnectionString [英] How can I reset the ConnectionString of EntityFramework .NET Core WebServer

查看:82
本文介绍了如何重置EntityFramework .NET Core WebServer的ConnectionString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Docker的常见设置:两个linux容器,一个是使用EntityFramework Core 2.2.6的.NET Core WebServer,另一个是MS-SQLServer2017。持久性数据存储在Docker卷中。使用 docker-compose ,它不是一群。

A common setup with Docker: Two linux containers, one a .NET Core WebServer using EntityFramework Core 2.2.6, the other a MS-SQLServer 2017. Persistent data is being held in a Docker volume. Using docker-compose, it's not a swarm.

启动SQLServer容器时,必须提供SA密码作为容器的环境变量。无论您提供了什么,以后都可以使用 docker container inspect 从容器外部读取此env。显然会损害安全性。

When starting the SQLServer container, one must provide the SA password as an environment variable to the container. However you provide that, it is possible to later read this env from outside the container using docker container inspect. Which obviously compromises security.

这使我想到两个问题:


  1. (在中进行讨论另一个线程有什么更好的方法可以向SQLServer提供SA密码?

  1. (discussed in another thread) What better ways are there to provide the SA password to the SQLServer?

Microsoft帮助指出这是最好的在启动容器后立即更改SA密码。当我在WebServer代码中执行此操作时,EntityFramework已经使用默认的SA密码(我作为env提供的密码)连接了。我可以轻松更改密码。但是如何告诉EntityFramework重置它的ConnectionString?

The Microsoft help states that it's best to change the SA password directly after starting the container. When I do that in my WebServer code, EntityFramework is already connected with the default SA password (the one I provided as env). I can change the password easily. But how can I tell EntityFramework to reset it's ConnectionString?

到目前为止,这是我的代码:

Here is my code so far:

    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddDbContext<API_DB_Context>(options =>
            {
                options.UseSqlServer(Configuration["AppSettings:ConnectionString"]);
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseAuthentication();
            app.UseMvc();

            using IServiceScope serviceScope = app
                                               .ApplicationServices
                                               .GetRequiredService<IServiceScopeFactory>()
                                               .CreateScope();
            var context = serviceScope.ServiceProvider.GetService<API_DB_Context>();
            var connection = context.Database.GetDbConnection();

            // Try connection.open() with changed PW. If that fails, use default PW.
            bool needToChangePW = CheckLoginPassword(connection);

            // Migration to (create and) update database
            context.Database.Migrate();

            // Change PW after migration (because maybe the DB didn't exist before)
            if (needToChangePW)
            {
                connection.Open();
                context.Database.ExecuteSqlCommand(string.Format("ALTER LOGIN SA WITH PASSWORD='{0}'", SA_CHANGED_PASSWORD));
                connection.Close();

                // Here I can set the ConnectionString in my Configuration.
                // But how can I get EntityFramework to actually use this updated string?!
                Configuration["AppSettings:ConnectionString"] = "modified string with new password"; 
            }
        }
    }


推荐答案

尝试一下

_dbContext.Database.GetDbConnection().ConnectionString = "your updated connection string";

在您的情况下:

var context = serviceScope.ServiceProvider.GetService<API_DB_Context>();
 var connection = context.Database.GetDbConnection();
connection.ConnectionString="modified string with new password";

这篇关于如何重置EntityFramework .NET Core WebServer的ConnectionString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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