0

I develop an application that must be used by anybody in the company, but not anybody has rights to access to the DB so I am impersonating a user with enough rights for accessing the DB part. However although I am using a username and password with enough privileges, even my personal account that has enough privileges, I get an authentication error

Login failed for user ''.

Any ideas? Below you can see my code.

PS1: for impersonation I am using the Matt Johnson's library as was mentioned here.

PS2: in the connection string I am using Integrated Security=false. When I was using Integrated Security=SSPI, the error message was

Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.

Code:

public static void test(string domain, string username, string password, LogonType type)
{
    string connectionString = @"Data Source=testsqlserver.company.com;Initial Catalog=TABLENAME;Integrated Security=false;Connection Timeout=240";

    using (Impersonation.LogonUser(domain, username, password, type))
    {
        using (SqlConnection sqlConnection = new SqlConnection(connectionString))
        {
            sqlConnection.Open();

            try
            {
                // Still not reached this point
            }
            catch (Exception exception)
            {
                string exceptionMessage = "Exception message:\n" + exception.Message.ToString() + "\nEnd of the exception message.";
                Console.WriteLine(exceptionMessage);
            }

            sqlConnection.Close();
        }
    }
}
Community
  • 1
  • 1
aragorn
  • 187
  • 3
  • 18

1 Answers1

0

I think I found the bug. I am not sure because I cannot test it since I do not have a user without enough priviledges in hand. The password contained a special character so I had to use the @ symbol when defining it.

I will come back with an update if this was not the fix.

aragorn
  • 187
  • 3
  • 18