C# 保护MySQL连接字符串

C# 保护MySQL连接字符串,c#,mysql,C#,Mysql,我正在设置一个来自C#应用程序的mysql连接,但我只是想知道这段代码是否提供了某种针对连接字符串攻击的安全性?我只是不想暴露太多的连接凭据,使它们容易受到攻击 protected MySqlConnection connection; protected string server; protected string database; protected string uid; protected string password; //Initialize values private

我正在设置一个来自C#应用程序的mysql连接,但我只是想知道这段代码是否提供了某种针对连接字符串攻击的安全性?我只是不想暴露太多的连接凭据,使它们容易受到攻击

protected MySqlConnection connection;
protected string server;
protected string database;
protected string uid;
protected string password;


//Initialize values
private void Initialize()
{
    server = "localhost";
    database = "databaseName";
    uid = "username";
    password = "password";
    var connectionString = string.Empty;
    connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

    connection = new MySqlConnection(connectionString.ToString());
}


//open connection to database
private bool OpenConnection()
{
    try
    {
        connection.Open();
        return true;
    }
    catch (MySqlException ex)
    {
        //When handling errors, you can your application's response based on the error number.
        //The two most common error numbers when connecting are as follows:
        //0: Cannot connect to server.
        //1045: Invalid user name and/or password.
        switch (ex.Number)
        {
            case 0:
                MessageBox.Show("Cannot connect to server.  Contact administrator");
                break;

            case 1045:
                MessageBox.Show("Invalid username/password, please try again");
                break;
        }
        return false;
    }
}

谢谢。

我没有发现漏洞。但除此之外,由于默认情况下使用连接池,因此应始终在可能的情况下
打开
关闭
连接。我将使用
using
-语句。你的
OpenConnection
方法建议你保持它的打开状态,我强烈建议你不要锁定或其他类型的问题(太多打开的连接等)。为什么不在你的
app.config
/
web.config
中保留你的连接字符串并加密它呢?谢谢你的信息,@TimSchmelter,我没有包括所有的代码,但事实上,我正计划根据需要打开和关闭连接。@DavidG这不是更容易暴露这些信条吗?我的意思是,这样做并不容易吗?我想这取决于加密方法和级别正确吗?@AJ152好吧,这比你在上面的代码中所做的更安全。对于某人来说,获取您的应用程序并在其中搜索用户名/密码并不太困难。例如,您可以使用以下应用程序: