我正在构建一个连接到需要SSL证书的服务器上的MySql数据库的网站。下面的 Ping 方法在我的本地PC上运行时无法正常工作但是当我将它部署到我们的Windows 2008 R2服务器时,我出现以下错误: 指定的网络密码不正确。。
我尝试使用完全相同的代码创建一个Windows窗体应用程序,并且在本地和Windows 2008 R2服务器上都可以正常工作。
为什么代码无法在共享服务器上的网站上运行?
我可以看到其他人遇到了同样的问题,并建议使用:
new X509Certificate2(Path, "", X509KeyStorageFlags.MachineKeySet);
但它没有帮助。也许我错误地使用了它!?!
private bool Ping()
{
string connectionString = "Server=xxx.xxx.xxx.xxx;Port=3306;Database=dbname;
Uid=server1_ssluser;Pwd=xxxxxxxx;SSL Mode=Required;
CertificateFile=c:\inetpub\xxxxx\App_Data\Certificates\server1\server1.pfx;"
try
{
using (MySqlConnection connection = new MySqlConnection(connectionString()))
{
connection.Open();
return true;
}
}
catch (Exception ex)
{
return false;
}
}
堆栈追踪:
System.Security.Cryptography.CryptographicException: The specified network password is not correct.
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromFile(String fileName, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx)
at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags keyStorageFlags)
at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName, String password)
at MySql.Data.MySqlClient.NativeDriver.GetClientCertificates()
at MySql.Data.MySqlClient.NativeDriver.StartSSL()
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.Driver.Open()
at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at MvcApplication1.Controllers.ValuesController.Ping()
答案 0 :(得分:0)
这可能不是实际问题,但看起来你没有正确地转义你的字符串。这一行:
string connectionString = "Server=xxx.xxx.xxx.xxx;Port=3306;Database=dbname;
Uid=server1_ssluser;Pwd=xxxxxxxx;SSL Mode=Required;
CertificateFile=c:\inetpub\xxxxx\App_Data\Certificates\server1\server1.pfx;"
应该是:
string connectionString = @"Server=xxx.xxx.xxx.xxx;Port=3306;Database=dbname;
Uid=server1_ssluser;Pwd=xxxxxxxx;SSL Mode=Required;
CertificateFile=c:\inetpub\xxxxx\App_Data\Certificates\server1\server1.pfx;"
否则,您将在字符串中获取\i
和\x
等控制代码。