Search code examples
mysqlasp.net-mvc-5

MySqlClient within .net MVC web app trying to connect as requesting host


I'm incredibly new to web apps so I apologize if the issue is caused by naivety, however, I'm at a complete loss.

I have a basic ASP.net core MVC app running on a ubuntu 20.4 server behind Apache2 and every time the app receives a request the Mysqlclient returns throws an exception of:

MySql.Data.MySqlClient.MySqlException (0x80004005): Authentication to host '' for user '' using method 'sha256_password' failed with message: Access denied for user ''@'{the requesting IP}' (using password: NO)

The connection string i'm expecting to be passed to the client is in-fact being passed with all of the correct parameters, which has been tested on my local machine(Win10). Both by running a local instance of MySQL and connecting remotely to the MySQL server on the ubuntu server, with both instances functioning correctly.

My only assumption is that the MysqlClient is somehow trying to use the webpage requesters credentials of ''@'{My IP}' which will obviously result in access being denied, but I'm not sure how or why this is happening.

my connection string is:

"SERVER=127.0.0.1; DATABASE=somedb; UID=someuser; PASSWORD=SomePassword;"

Then my connection request is made with the below, without any amendments to the default MySqlConnection object:

private static MySqlConnection dbConnection;
dbConnection = new MySqlConnection(connectionString);

The errors I'm getting from the application are:

fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
      An unhandled exception has occurred while executing the request.
MySql.Data.MySqlClient.MySqlException (0x80004005): Authentication to host '' for user '' using method 'sha256_password' failed with message: Access denied for user ''@'{my IP}' (using password: NO)
 ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Access denied for user ''@'{my IP}' (using password: NO)
   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.NativeDriver.ReadPacket()
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacket()
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.AuthenticationFailed(Exception ex)
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacket()
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ContinueAuthentication(Byte[] data)
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.HandleAuthChange(MySqlPacket packet)
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.Authenticate(Boolean reset)
   at MySql.Data.MySqlClient.NativeDriver.Authenticate(String authMethod, Boolean reset)
   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()

Solution

  • I think your connection string might be incorrect, i'm no MySql expert but according to some Google results 'PASSWORD' should be 'Pwd', and the fields are PascalCase. Also you might need to specify a TCP port.

    Example connection string:

    Server=127.0.0.1;Port=1234;Database=somedb;Uid=someuser;Pwd=somePassword;