"Login failed for user..." when I log on to my online SQL Server using C#?

667 Views Asked by At

I have a database on a hosting server that I try to connect using C# SqlConnection class. I can log on to the server through SQL Server Management Studio using SQL Server authentication without problem.

My initiation code looks like this:

public static string ConnectionString { get; set; }

public Constructor() {
    ConnectionString = @"Data source=SQL6002.site4now.net;Initial Catalog=myDatabase;User Id=MyUserName;Password=MyPassword;";
}

where database name, user name and passwords are replaced with the proper values. The connection code looks like:

using (SqlConnection con = new SqlConnection(ConnectionString))
{
    con.Open();
    ...
}

This code works when I'm using a local db connection string. However, when I'm using the connection string above I get the error

An error has occurred. Login failed for user 'IFC\WINxxxx$'.

ExceptionType: System.Data.SqlClient.SqlException
StackTrace

at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, etc. etc.

where x in the user name are numbers. Also, this isn't the user name I use in the connection string.

I have tried to add trusted_connection=true, and also integrated security=true without success.

When I log on to the server via SQL Server Management Studio and open properties in Security -> Logins -> MyUserName, I note that the password does not have the same length as the one I use to log on to SSMS with. I do not have permission to change it through SSMS, only through the host, smarterasp.net.

Any inputs are very welcome!

1

There are 1 best solutions below

0
On

Thanks for the comments. I moved the credentials to an instance of SqlCredentials, as

var credentials = new SqlCredentials(userName, password);
using (var con = new SqlConnection(ConnectionString, credentials){
    con.Open();
}

where password is a readonly SecureString(). That did the trick!