Search code examples
sqlsql-serversqlconnection

Error : Login failed for user 'domain\username' using SqlCredential object


I am using below mentioned code when I tried to connect with SQL server. I am geeting Login failed for user 'domain\username'. I have checked that the user having sysadmin permission in SQL Server & I am using SQL server Express edition.

connection string I used for this "Initial Catalog=Employee;Server=serverName"

 public static bool connectSqlClient(string connecton)
     {
         bool isConnect = false;
         try
         {
             string username = @"domain\username";
             string initString = "abcpassowrd";
             // Instantiate the secure string.
             SecureString testString = new SecureString();

             // Use the AppendChar method to add each char value to the secure string. 
             foreach (char ch in initString)
                 testString.AppendChar(ch);

             testString.MakeReadOnly();
             SqlCredential cred = new SqlCredential(username, testString);
             SqlConnection conn = new SqlConnection(connecton, cred);
             conn.Open();
             isConnect = true;
         }
         catch (Exception)
         {
             throw;
         }

         return isConnect;
     }

Let me know if I missed something.


Solution

  • Typically, when you add a Login to Sql Server, there are 2 modes.

    Sql Server Authentication (which is the "old school" user-name and pwd scenario)

    and

    "Windows Authentication". which is where you find a user (domain\username) on your network and add the login. THIS SCENARIO DOES NOT REQUIRE A PASSWORD TO BE SET/SENT.

    Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
    

    That is a typical "Windows Authentication" connection string. You don't set the username/pwd, because the IIDentity is "picked up" from who/whatever is logged into or whose credentials the application is running.

    I ~think you want to use Windows-Authentication (since you mention 'domain/username')....but you're setting the pwd as if you were using Sql-Server-Authentication. Thus "mixing" the 2 models.

    as mkross mentions, just because you add the LOGIN, you still need to "link in" the database itself. If you go to the Security/Logins/ (properties) and go the "User Mapping" selection, you can "Map" to the database, and select a role_membership like "db_datareader" as a low rights role.

    APPEND:

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcredential%28v=vs.110%29.aspx

     SqlCredential provides a more secure way to specify the password for a login attempt using SQL Server Authentication.
    
    SqlCredential is comprised of a user id and a password that will be used for SQL Server Authentication.
    

    So yeah, that's for Sql-Server-Authentication. Not Windows-Authentication.

    you probably just need

    string myConnectionString = "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;"