System.Data.SqlClient.SqlException: Login failed for user 'NT AUTHORITY\NETWORK SERVICE'
|
|
|
|
|
The title of this article describes a very common exception developers run into when trying to connect to a database or
trying to access a resource across network. Lot of time developer will say that ASP.Net was working fine on my development
environment but when it got deployed on production server or deployed on a new server the following exception was thrown
by the application.
Server Error in '/portal' Application.
--------------------------------------------------------------------------------
Login failed for user 'NT AUTHORITY\NETWORK SERVICE'.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Login failed for user
'NT AUTHORITY\NETWORK SERVICE'.
Source Error:
An unhandled exception was generated during the execution of the current web
request. Information regarding the origin and location of the exception can
be identified using the exception stack trace below.
Stack Trace:
[SqlException (0x80131904): Login failed for user 'NT AUTHORITY\NETWORK
SERVICE'.]
--------------------------------------------------------------------------------
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.
|