我正在建立一个网站,该网站正在连接到需要SSL证书的服务器上的MySql数据库。在本地计算机上运行以下Ping方法时,它无法顺畅地工作,但是当我将其部署到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()
最佳答案
这可能不是实际的问题,但看起来您没有正确地转义字符串。这行:
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
之类的控制代码。关于.net - .net SSL pfx->指定的网络密码不正确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35697734/