Login failed for user computer\ASPNET

created at 01-08-2022 views: 4

problem

My database connection string is like this:

server=MachineName\SQLEXPRESS;database=test;trusted_connection=true

I feel very strange. When the web service is deployed and run alone, there is no problem, but when it is published to IIS, there is a problem with access. I checked the following information, as follows:

SYMPTOMS

When you create a trusted connection from Microsoft ASP.NET to Microsoft SQL Server, you may receive the following error message:

Login failed for user 'MachineName\ASPNET'

For computers that run Internet Information Services (IIS) 6.0, you may receive the following error message:

Login failed for user 'NT AUTHORITY\NETWORK SERVICE'

Note You receive either of these error messages specifically when you use integrated security (when you include the integrated security=sspi attribute in a connection string).

RESOLUTION

To resolve this issue, use one of the following methods:

  • Method 1: Programmatically change the security context of the ASP.NET worker process to a user who has the correct SQL Server permissions.
  • Method 2: Change the default configuration of ASP.NET so that the ASP.NET worker process starts and runs under the context of a user who has the correct permissions in SQL Server.
  • Method 3: Grant the correct permissions in SQL Server so that the ASPNET account (or NetworkService account, for an application that runs on IIS 6.0) has the appropriate access to the required resources.

Note This method will make all the Web applications on the server have the corresponding right on the computer that is running SQL Server.

I use the third method, add an ASPNET user in SQL SERVER, and then give certain permissions, because my web service is only for learning purposes, so the permissions given are sysadmin. The method of adding is as follows:

  1. Enter Microsoft SQL Server Management Studio Express, right-click Loings under Security, and create a new Login
  2. Create a new MachineName \ASPNET, MachineName is your machine name
  3. Set options such as Server Roles

This error can take a variety of forms, including:

  • "Login failed for user '(null)'"
  • "Login failed for user domain\username"
  • "Login failed for user computer\ASPNET"

The likely cause is that you are working with a Web application and are trying to access a SQL Server. The specific error or exception that is thrown depends on whether the SQL Server is on the same computer as the Web server or on a separate computer. This problem arises because the Web application is not passing proper credentials to the SQL Server. In general, the solution is to:

  • If the SQL Server is on the same computer as the Web server, give the local ASPNET user login privileges on the SQL Server.
  • Set IIS or ASP.NET to impersonate a Windows domain user who has login privileges on the SQL Server.

    If you want to authenticate your users in DataSource, you need add to web.config in your web-application:

    <identity impersonate="true"/> and use "Windows integrated security" in your DataSource configuration. As result, your users will be authenticated in you database.

  • Use a connection string with an explicit user ID and password. change the auto generated connection string in web.config file from

<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\myDatabaseName.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>

To

<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\myDatabaseName.mdf;User ID=sa;Password=mySAPassword;database=myDatabaseName" providerName="System.Data.SqlClient"/>
Please log in to leave a comment.