Search code examples
c#sql-serverwindows-8.1windows-server-2012iis-8.5

Connecting IIS to SQL Server 2014 on Windows Server 2012


I keep getting the error

'Cannot open the database "mydb" requested by the login. The Login failed. Login failed for user "NT Authority\NETWORK SERVICE"'

This is my connection string

SqlConnection _connection = new SqlConnection("Data Source='MYSERVER\\SE';Initial Catalog=mydb;Integrated Security=True;MultipleActiveResultSets=True; Trusted_Connection=Yes");

The same web form can connect locally to SQL server on windows 8.1.

Why is this?


Solution

  • In your connection string, Integrated Security=True is causing it to attempt to login to the SQL Server using the account that IIS runs with.

    Based on what you have described, your 8.1 box has IIS running with an account that has permission to access your SQL Server.

    You can add the Network Service account to your SQL Server as a valid login, change the account that IIS launches with on the server to an account that already has access to the SQL Server, or specify an SQL login in your connection string instead of using Integrated Security.