Login failed for user computer\ASPNET

我的数据库连接字符串是这样的:server=MachineName\SQLEXPRESS;database=test;trusted_connection=true

感到很是奇怪,当单独部署运行web service时没问题,但把它发布到IIS, 访问就有问题了,查了下资料,如下:

 

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.

 

我采用的是第3种方法,在SQL SERVER中增加一个ASPNET的用户,然后赋予一定的权限,因为我的web service仅做学习用途,所以赋的权限是sysadmin。添加方法如下:

1.进入Microsoft SQL Server Management Studio Express, 右击Security下的Loings,新建一个Login

2. 新建一个MachineName \ASPNET, MachineName 为你的机器名

3. 设置 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"/>

 

For more information, see Accessing SQL Server from a Web Application.

 

 

from:

http://msdn.microsoft.com/en-us/library/aa984220(VS.71).aspx

posted @ 2012-09-25 18:35  emanlee  阅读(2160)  评论(0编辑  收藏  举报