【问题标题】:Which way is better for MySQL connection string?哪种方式更适合 MySQL 连接字符串?
【发布时间】:2021-04-06 16:01:12
【问题描述】:

我有一个连接到 MySQL 数据库的 C# 程序。我原来的 connectionString 嵌入在下面的代码中

public class RemoteDatabaseConnection
{
    private MySqlConnection remoteConnect;
    private string conStr;
    
    public RemoteDatabaseConnection()
    {
        conStr = "server=" + AppSetting.serverIP + ";user id=ABCD;password=XYZERT;Database=" + AppSetting.databaseName;
    }

    public void Connect()
    {
        try
        {
            remoteConnect = new MySqlConnection(conStr);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Connect Failed\n" + ex.Message);
        }
    }
}

Appsetting.serverIP和AppSetting.databaseName是从ini文件中加载的,可以修改。

但我发现有人在 app.config 中使用了 connectionString。喜欢

<connectionStrings>
    <add name="conStr" connectionString="server=192.168.0.245;user id=ABCD;password=XYZERT;Database=service_report"/>
</connectionStrings>

谁能解释哪种方式更好更安全?

如果我想在app.config中使用connectionString,怎么会变成conStr = "server=" + AppSetting.serverIP + ";user id=ABCD;password=XYZERT;Database=" + AppSetting.databaseName;" ? 因为我的serverIP和databaseName不固定。

【问题讨论】:

  • 硬编码用户名和密码总是一个坏主意。您可以轻松更改配置设置,甚至对其进行加密。 my serverIP and databaseName is not fixed. 这是什么意思?每次运行应用程序时,甚至在应用程序运行时,服务器都会发生变化吗?
  • 顺便说一句,RemoteDatabaseConnection 类是一个等待发生的死锁。与 ADO.NET 的连接类和 no 处理连接的方式相比,它没有提供任何好处。连接应该是短暂的,在 using 块中声明,以便在使用后立即处理它们。这样,服务器上任何剩余的锁和资源都会被释放。 ADO.NET 使用连接池来降低打开新连接的成本。
  • 如果您想使您的代码与不同的数据库产品一起工作,请使用抽象基类,例如 DbConnection、DbCommand,而不是像 MySqlConnection 或 SqlServerConnection 这样的具体类。也可以使用DB Provider model通过配置指定数据库产品。
  • "my serverIP and databaseName is not fixed" 表示我在不同的地方使用这个程序,所以这个工厂 ip 可能是 211.220.123.22,下一个工厂 ip 是 123.222.52.43。但是我编码时不知道IP地址。
  • 在这种情况下,您可以在每台机器上的app.config 中存储不同的设置。所有这些内容都在 ADO.NET 教程、书籍和课程中进行了描述。不要试图猜测你的方式。仅这个类就会导致数据库的严重性能问题,而不仅仅是应用程序。如果您连接字符串以在另一点创建查询,您将允许 SQL 注入攻击

标签: c# mysql


【解决方案1】:

有一个完整的文档讨论这个话题。 Official microsoft documentation。另一件事是,我不认为直接在代码中嵌入连接字符串是好的做法。即使您隐藏了您的服务器 IP 和数据库名称,您也会暴露您的用户 ID 和数据库密码,这可能会很糟糕。存储机密的最安全方法是在 .env、app.config 中或将其存储在某种云服务中,例如 Azure Key Vault。 Azure Key Vault

【讨论】:

    【解决方案2】:

    如果你想使用 Entity Framework Core 你可以这样使用:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
         #warning To protect potentially sensitive information in your connection string, 
         you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 
         for guidance on storing connection strings.
         
       optionsBuilder.UseMySQL("server=localhost;database=library;user=user;password=password");
    }
    

    而且最好将 ConnectionString 存储在 appconfig.json 中。 您可以在 Startup.cs 中的 ConfigureServices 上进行设置:

    services.AddDbContext<DataContext>(options => options.UseMySql(Configuration.GetConnectionString("DataContext")));
    

    查看此链接以获得更多帮助:

    mySQL.com help Entity Framework Core Support

    mySQL.com help Creating a Connector/NET Connection String

    【讨论】:

      猜你喜欢
      • 2012-06-27
      • 1970-01-01
      • 2017-03-15
      • 2012-11-05
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      相关资源
      最近更新 更多