简体   繁体   中英

Connection to SQL Server fails if executed from a Windows Service

I've got a little C# app that drops and re-created a database for my tests to run. This app is executed in TeamCity that runs under NT AUTHORITY\\SYSTEM user.

Here is my application:

    public void SetupDatabase()
    {
        var childConnectionString = "Server=.\sqlexpress;Database=MyDatabase;Uid=Tester;Pwd=Tester;Trusted_Connection=True;MultipleActiveResultSets=True;";

        var masterConnectionString = "Server=.\sqlexpress;Database=master;Uid=Tester;Pwd=Tester;Trusted_Connection=True;MultipleActiveResultSets=True;";

        Console.WriteLine("Using child connectionString: {0}", childConnectionString);
        Console.WriteLine("Using Master connectionString: {0}", masterConnectionString);

        Console.WriteLine("Connection for master using this user: {0}", GetUser(masterConnectionString)); // <- this one shows NT AUTHORITY\SYSTEM

        Console.WriteLine("Connection for child using this user: {0}", GetUser(childConnectionString)); // <- this one fails saying 
        // Cannot open database "MyDatabase" requested by the login. The login failed.
        // Login failed for user 'NT AUTHORITY\SYSTEM'.
    }

    private string GetUser(string connectionString)
    {
        using (var connection = new SqlConnection(connectionString))
        {
            connection.Open();

            var command = new SqlCommand("select SYSTEM_USER", connection);

            var reader = command.ExecuteReader();

            while (reader.Read())
            {
                var user = reader.GetString(0);
                return user;
            }
        }
        return "";
    }

I'm specifying SQL Server username/password in the connection string, why is Login failed for user 'NT AUTHORITY\\SYSTEM' ?? I'm not using Windows Authentication.

Fair enough, System account will fail to login to MyDatabase because it is not set up on that database.

Funny thing - when I execute the same application myself I get no exceptions and my username is printed

Connection for master using this user: MACHINENAME\trailmax
Connection for child using this user: MACHINENAME\trailmax

Why-oh-why is username/password from the connection string are ignored and trying to login as Windows-Auth.

ps the user Tester is set up on MyDatabase and when I go to SSMS and use credentials from the connection string, I can login and can execute all the commands I need. The problem appears when I run the same executable from TeamCity.

You set Trusted_Connection=True , which means use windows authentication. If you want SQL authentication, then set Trusted_Connection=False .

从连接字符串中删除trusted_connection=true

Take Trusted_Connection=True; out of your connection string. This overrides any user name or password.

Then have a think about your security architecture - do you really want to be hard coding and compiling logins and passwords into your windows servce?

Remove trusted_connection=true from your connection string as that is gonna override the user name you have configured. In the case of Windows Services, it's gonna use NT AUTHORITY\\SYSTEM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM