My database connection string is like this:
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:
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).
To resolve this issue, use one of the following methods:
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:
Security, and create a new
MachineNameis your machine name
This error can take a variety of forms, including:
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:
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"/>
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\myDatabaseName.mdf;User ID=sa;Password=mySAPassword;database=myDatabaseName" providerName="System.Data.SqlClient"/>