Tuesday, December 01, 2009

System.Data.SqlClient.SqlException: Login failed for user 'NT AUTHORITY\NETWORK SERVICE'

Login failed for user 'NT AUTHORITY\NETWORK SERVICE' hibara itt van egy leiras a netomatrix-rol:

Dependig on the operating system this user account is either Network Service or ASPNET. On Windows 2003 ASP.Net process runs under context of 'Network Service' account and on Windows XP or prior the process runs under context of 'ASPNET' user account.

The error message gives all the clues that you need to figure out what the problem is. Your application is trying to open connection with SQL server and SQL Server is not authenticating the user context. ASPNET or Network Service is not some user account that you could have created. So first clue is that it is some built-in windows account that is trying to connect to SQL Server. That tells you that your connection string is using Integrated Security meaning it wants to use the login user's credentials to connect to SQL Server. This leads to next clue that you intended to use Windows authentication for ASP.Net application. Based on this forensic data and clues following is a list of things that could have gone wrong.

  • Your connection string is wrong. Meaning you wanted to use SQL authentication but for some reason it is using Integrated Security
  • Your settings in Web.config are wrong. Meaning that you wanted to use authentication mode to be windows but it is set to none.
  • Your IIS settings for web application or virtual directory are not correct.
  • You forgot to add these Windows accout to SQL Server users list

Based on above list, you can do the following to fix the problem.

  • If you intended to use SQL authentication and not Integrated security then fix your connection string.
  • Fix authentication mode in Web.config and set impersonation on if you intended to use Windows authentication for ASP.Net web application. Also make sure that you have turned off anonymous access in IIS for your ASP.Net web application
  • You can grant 'Network Service' or 'ASPNET' user accounts permissions to connect to database.