我遇到以下错误:
Cannot open database "testbase" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\Web2'.
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: Cannot open database "testbase" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\Web2'.
那么,我可以在哪里给这个用户的Web2权限? (顺便说一下,服务器没有这样的用户Web2但在wwwroot文件夹中有一个名为Web2的文件夹。)
我寻找答案,但都失败了,如下所示:
[1]将用户IUSR添加到该文件夹并授予其读取权限。
[2] http://www.codekicks.com/2008/11/cannot-open-database-northwind.html
[3] http://blog.sqlauthority.com/2009/08/20/sql-server-fix-error-cannot-open-database-requested-by-the-login-the-login-failed-login-failed -用于用户-NT-authoritynetwork服务/
您需要使用用户名“IIS APPPOOL \ Web2”将用户添加到SQL Server,并为该用户提供对数据库的访问权限。 请注意,您不会在search中看到此用户名,但如果您点击“查看名称”,则可能会看到它改为加下划线。
除了pipe理应用程序池的权限之外,用户并不存在于机器上。
或者,您可以将应用程序池设置为以不同的用户身份运行。
有关更多信息,请参阅http://blogs.iis.net/webdevelopertips/archive/2009/10/02/tip-98-did-you-know-the-default-application-pool-identity-in-iis-7 -5-windows-7-changed-from-networkservice-to-apppoolidentity.aspx和http://learn.iis.net/page.aspx/624/application-pool-identities/
这里是一个SQL Serverlogin列表的例子:

这里是一个数据库的login列表的例子:

以下是数据库用户的详细信息:(注意两个框的名称相同,第二个框必须与SQL Serverlogin列表中的login名匹配)。

您也可以尝试使用代码而不是GUI:
USE [master] GO CREATE LOGIN [IIS APPPOOL\Web2] FROM WINDOWS WITH DEFAULT_DATABASE=[testbase] GO USE [testbase] GO CREATE USER [IIS APPPOOL\Web2] FOR LOGIN [IIS APPPOOL\Web2] GO EXEC sp_addrolemember N'db_owner', N'IIS APPPOOL\Web2' GO
我分配了所有者权限,但您的应用程序可能能够以较低的权限脱身。 通常认为给予最低的权限是很好的做法。