要点:
我得到了“无法连接到任何指定的MySQL主机,之前正在运行的代码'尝试通过ASP.NET Web应用程序连接到MySQL数据库时出错。完整错误如下。
奇怪的是,我在一个临时的新网站项目中开发了应用程序,一切正常。但是,当我将其用于我尝试实施的网站的离线版本时,会出现此错误。有趣的是,我实际上使用的是已在网站中设置并正常运行的连接字符串。所以我很困惑为什么我的代码在其他地方运行,并且连接字符串在其他地方运行,但当我尝试一起使用它们时,我得到了“无法连接”#39;错误。
我尝试过的事情:
第一件事显然是检查我的连接字符串格式是否正确。我对此进行了仔细检查,但正如我所说,连接字符串已经(并且仍然可以)与网站上的其他页面一起使用。
我还看到了一些有关此错误的帖子,建议我在web.config文件中包含该标记。然而,这并没有解决我的问题。
我还验证了我的MySQL数据库已启动且可运行。如上所述,我可以使用连接字符串来处理页面的其他方面。
代码段:
完整错误:
MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts. ---> System.Security.SecurityException: Request for the permission of type 'System.Net.SocketPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.Net.Sockets.Socket.CheckCacheRemote(EndPoint& remoteEP, Boolean isOverwrite)
at System.Net.Sockets.Socket.BeginConnectEx(EndPoint remoteEP, Boolean flowContext, AsyncCallback callback, Object state)
at System.Net.Sockets.Socket.BeginConnect(EndPoint remoteEP, AsyncCallback callback, Object state)
at MySql.Data.Common.StreamCreator.CreateSocketStream(IPAddress ip, Boolean unix) at MySql.Data.Common.StreamCreator.GetStream(UInt32 timeout)
at MySql.Data.MySqlClient.NativeDriver.Open()
The action that failed was: Demand
The type of the first permission that failed was: System.Net.SocketPermission
The first permission that failed was:
The demand was for:
The granted set of the failing assembly was:
The assembly or AppDomain that failed was: MySql.Data, Version=6.2.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d
The method that caused the failure was: System.IO.Stream CreateSocketStream(System.Net.IPAddress, Boolean)
The Zone of the assembly that failed was: Intranet The Url of the assembly that failed was: file://PROAPPSRV/inetpub/wwwroot/MyChiller/bin/MySql.Data.DLL -
-- End of inner exception stack trace ---
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 TCCVC_Login.ValidateUser(Object sender, EventArgs e)
C#MySQL连接器:
using (MySqlConnection conn = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["newrst110ConnectionString"].ConnectionString))
{
using (MySqlCommand cmd = conn.CreateCommand())
{
try
{
cmd.Connection = conn;
conn.Open();
cmd.CommandText = @"SELECT custpass AS hash, (lastlogin IS NOT NULL) AS firstLogin FROM rts110.mychiller_users
WHERE custusername = @user";
cmd.Parameters.AddWithValue("@user", Login1.UserName);
MySqlDataReader reader = cmd.ExecuteReader();
DataTable returnTable = new DataTable();
returnTable.Load(reader);
reader.Close();
// UPDATE LAST LOGIN DATE
cmd.CommandText = @"UPDATE mychiller_users
SET lastLogin = CURDATE()
WHERE custusername = @userName";
cmd.Parameters.AddWithValue("@userName", Login1.UserName);
cmd.ExecuteNonQuery();
cmd.Dispose();
// . . .
// Additional code down here that handles the return and closes the connection.
连接字符串:
<add name="newrst110ConnectionString" connectionString="Datasource=70.103.118.100;Database=rts110;uid=####;pwd=####;" providerName="MySql.Data.MySqlClient"/>
当个别组件都能正常运行时,是否有人知道为什么我遇到此错误?
提前感谢您的帮助。
更新
我一直在修补这个问题,看看能不能解决问题。我注意到当我创建测试页面并在下面创建基本连接器时,我得到了一个应用程序安全性错误,也在下面。
简单连接器
DataTable returnTable = new DataTable();
using (var conn = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["workorderConnectionString"].ConnectionString))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
try
{
cmd.CommandText = @"SELECT * FROM workorder.jobnumber";
MySqlDataReader reader = cmd.ExecuteReader();
returnTable.Load(reader);
reader.Close();
cmd.Dispose();
}
catch (Exception ex)
{
lbltest.Text = ex.ToString();
}
finally
{
conn.Close();
}
}
}
应用程序错误:
Server Error in '/' Application.
Security Exception
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.
Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Net.SocketPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[SecurityException: Request for the permission of type 'System.Net.SocketPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
System.Security.CodeAccessPermission.Demand() +54
System.Net.Sockets.Socket.CheckCacheRemote(EndPoint& remoteEP, Boolean isOverwrite) +270
System.Net.Sockets.Socket.BeginConnectEx(EndPoint remoteEP, Boolean flowContext, AsyncCallback callback, Object state) +175
System.Net.Sockets.Socket.BeginConnect(EndPoint remoteEP, AsyncCallback callback, Object state) +179
MySql.Data.Common.StreamCreator.CreateSocketStream(IPAddress ip, Boolean unix) +239
MySql.Data.Common.StreamCreator.GetStream(UInt32 timeout) +650
MySql.Data.MySqlClient.NativeDriver.Open() +366
基于我的谷歌搜索,这再次指回Web.Config文件中的标记,我已经包含在那里。也许我不正确地包括这个?
我想知道它是否也可能与我拥有的两个Web.config文件有关(一个在带有成员页面的子文件夹中,以限制没有经过验证的会话的访问)。我尝试添加两者并没有看到任何区别。
答案 0 :(得分:0)
你能附上你的连接字符串的版本吗?似乎您的配置文件可能存在配置错误。
答案 1 :(得分:0)
在对应用程序错误进行进一步研究后,我得到了简化的连接字符串,我偶然发现了以下内容:
The application attempted to perform an operation not allowed by the security policy
在查看该问题的答案后,我将解决方案移至本地驱动器,因为我是从服务器文件夹运行它。这样做解决了这个问题,现在一切都很完美。