17

I have created a windows service & my service is dependent on the SQL server service. First, SQL started and then my service started when starting PC or restarting PC.

This works fine but the issue is database file can not be read by the Windows service, throwing the exception "Cannot open user default database. Login failed.Login failed for user 'NT AUTHORITY\SYSTEM' " when i am trying to read the database.

If I start the computer & login then it can not read the database, but if i have start the computer & wait for a few seconds and then log into the window service it reads the database.

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
user847455
  • 781
  • 4
  • 9
  • 22
  • http://stackoverflow.com/questions/2251839/login-failed-for-user-nt-authority-network-service – incomplete Feb 12 '13 at 09:50
  • try this one http://blog.sqlauthority.com/2009/08/20/sql-server-fix-error-cannot-open-database-requested-by-the-login-the-login-failed-login-failed-for-user-nt-authoritynetwork-service/ – reza.cse08 Mar 01 '16 at 10:11

6 Answers6

33

I bet you have this in your connection string:

Integrated Security=SSPI

or something similar.

Now the account that service is running under (NT AUTHORITY\SYSTEM) tries to connect to the database - and can't since it's not authorized to do so.

You can:

  • either create a login for NT AUTHORITY\SYSTEM in your SQL Server and give it the necessary permissions it needs for your app

OR:

  • you create a specific application user account in SQL Server (login to SQL Server and user in your database) and change your connection string to:

    User ID=(your app account);pwd=YourPassword
    
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • string conString = "Data Source = .\\SQLEXPRESS;AttachDbFileName = " + Path.path + "; Integrated Security =true;Connect Timeout=1000" – user847455 Aug 01 '11 at 07:24
  • @user847455: yes that's what I thought - Integrated Security. I mentioned possible solutions above - pick one – marc_s Aug 01 '11 at 15:12
  • I picked your first option. This code adds the permission: `var process = new Process(); var startInfo = new ProcessStartInfo { WindowStyle = ProcessWindowStyle.Hidden, FileName = "cmd.exe", Arguments = "/C sqlcmd -S .\\SQLEXPRESS -Q \"sp_addsrvrolemember NT AUTHORITY\\SYSTEM\"" }; process.StartInfo = startInfo; process.Start();` – sinaptik Oct 07 '14 at 19:55
8

Go to security tab under your database.

Under security, Click to expand the Users tab.

Find weather NT AUTHORITY\SYSTEM is present.

If not, Right click on users, Click on new user

In new user window, general tab change the user type to windows user.

Select the User name to, NT AUTHORITY\SYSTEM you will have to find it in the Advanced tab, find now, and select system.

Similarly for login name, browse and find NT AUTHORITY\SYSTEM.

And in the membership tab to the left, select all the required boxes and click ok.

Probably this would fix your issue. Hope this helps.

Prashanth Benny
  • 1,523
  • 21
  • 33
  • 1
    Security->Logins: select `NT AUTHORITY\SYSTEM` Login Properties -> User Mapping and then you can add the database(s) you want to give permissions to, and set role membership for this user. Thank you!! – Scott Fleming Oct 29 '20 at 15:17
3

I removed Trusted_Connection=yes from connection string and it worked perfectly.

ramya
  • 2,350
  • 6
  • 31
  • 57
1

I actually just changed the permissions for the NT AUTHORITY\SYSTEM user in SQL Server and this worked for me.

Adam Levitt
  • 10,316
  • 26
  • 84
  • 145
1

you can add this on Connection string

persist security info=True

Shimith Vk
  • 11
  • 1
-2

The simple solution is to check your web.config file and make sure one of these is part of the database connection string:

Trusted Connection=false

OR

Integrated Security=True
Raj Baral
  • 661
  • 6
  • 19
Samnan
  • 653
  • 1
  • 5
  • 13
  • System.ArgumentException: Keyword not supported: 'trusted connection'. – AminM Apr 05 '13 at 13:53
  • This is not a simple solution this is exactly the problem of the thread opener... that he has Integrated Security=true – Pascal Jan 25 '14 at 15:27