C# .Net MysqlConnector ConnectionString Password Problem / Issue

Viewed 645

need help with some stupid password problems. Following problem:

Cannot connect to mysql server with some passwords ^^

If i use a password with the same "connection string" details, for example:

The following Passwords are exactly in the MySql Server (quotes, double-quotes, semicolon);

"pwd=123456"; // works

"pwd=123456;"; // works

";pwd=123;456;' // System.ArgumentException

";pwd=123;456;'" // System.ArgumentException

//Connection String Variant 1:

string connString = string.Format("server={0};port={1};uid={2};pwd={3};database={4};sslmode=preferred",
                tbDbServername.Text.ToLower(),
                (uint)Convert.ToInt32(tbDbServerPort.Text),
                tbDbServerUsername.Text.ToLower(),
                tbDbServerPassword.Password,
                tbDbServerDatabase.Text.ToLower()
                );

//Connection String Variant 2:

string connString = string.Format("server={0};port={1};uid={2};pwd='{3}';database={4};sslmode=preferred",
                tbDbServername.Text.ToLower(),
                (uint)Convert.ToInt32(tbDbServerPort.Text),
                tbDbServerUsername.Text.ToLower(),
                tbDbServerPassword.Password,
                tbDbServerDatabase.Text.ToLower()
                );

I know it's stupid. Why should someone use a password with connection string details....but would be nice to solve this.

Greetings from Germany :)

2 Answers

Use a MySqlConnectionStringBuilder to construct your connection string. It will do any escaping/quoting that's needed.

In your case, you would want to do this:

MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder()
{
    Server = tbDbServername.Text.ToLower(),
    Port = (uint)System.Convert.ToInt32(tbDbServerPort.Text),
    UserID = tbDbServerUsername.Text.ToLower(),
    Password = tbDbServerPassword.Password,
    Database = tbDbServerDatabase.Text.ToLower(),
    SslMode = MySqlSslMode.Preferred
};

string connString = builder.ConnectionString;

Certain characters are not allowed in connection string like [] {}() , ; ? * ! @ =. without using escape-character. The use of ; as part of password has caused problem as its expected to be used as delimiter

The special notes on special characters is mentioned at relatively odd place in Microsoft documentation:

Notes on special character

If used in an OLE DB or ODBC connection string, a login or password must not contain the following characters: [] {}() , ; ? * ! @ =. These characters are used to either initialize a connection or separate connection values.

ConnectionString

The basic format of a connection string includes a series of keyword/value pairs separated by semicolons. The equal sign (=) connects each keyword and its value. To include values that contain a semicolon, single-quote character, or double-quote character, the value must be enclosed in double quotation marks. If the value contains both a semicolon and a double-quote character, the value can be enclosed in single quotation marks. The single quotation mark is also useful if the value starts with a double-quote character. Conversely, the double quotation mark can be used if the value starts with a single quotation mark. If the value contains both single-quote and double-quote characters, the quotation mark character used to enclose the value must be doubled every time it occurs within the value.

Related