【问题标题】:IIS SQL Server configurationIIS SQL 服务器配置
【发布时间】:2014-06-30 20:58:31
【问题描述】:

我有一个 C# ASP.net 项目,它是使用 Visual Studio for web 2013 和 SQL Server express 2012 开发的。

我使用的数据库连接线如下:

SqlConnection objConnection = new SqlConnection("Data Source=.\\MYDB;Initial Catalog=MYDB;Integrated Security=true");
        objConnection.Open();

这在 Visual Studio 的开发人员模式下运行良好 - 但现在我已部署到在用户 IIS_IUSRS(Andy-PC\IIS_IUSRS) 下运行的 IIS。

当我访问我的页面时,他们会抛出以下错误:

Server Error in '/' Application.

Cannot open database "MYDB" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\MYDB'.

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 "MYDB" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\MYDB'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[SqlException (0x80131904): Cannot open database "MYDB" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\MYDB'.]
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +6749670
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +815
   System.Data.SqlClien

它非常不言自明 - 我的带有集成 security=true 的连接字符串无法成功运行,因为当前登录的用户不是 IIS_IUSRS。

有人可以帮忙说明我应该将连接字符串更改为什么,以便在以这个新的 IIS 用户身份连接时创建连接吗?

【问题讨论】:

  • 您是否在连接字符串中使用了 Windows 身份验证?
  • Data Source=.\\MYDB 不是说你在访问本地服务器吗?因此,如果您将其部署在其他地方,您可能需要将点 (.) 替换为真实的服务器名称或 IP 地址

标签: c# asp.net sql-server iis sql-server-2012


【解决方案1】:

您需要更改运行应用程序池的用户。

转到应用程序池的高级设置并将身份更改为可以访问您的 sql 服务器的用户。

【讨论】:

    【解决方案2】:

    实际上您可以使用该连接字符串,只需在您的 SQL 服务器和数据库中授予IIS APPPOOL\MYDB 的权限即可。

    【讨论】:

      【解决方案3】:

      从长远来看,为网站使用明确的帐户可能会更容易管理安全性,例如在您的 ASP .NET 项目的 Web.config 文件中放置一个连接字符串,例如:

      <add name="SiteDB"
        connectionString="Data Source=HostName\SQLServerInstance; Initial Catalog=MyDB; User Id=MyFirstWebsite; Password=secret"
        providerName="System.Data.SqlClient"/>
      

      然后使用ConfigurationManager获取连接字符串。

      当服务器上有多个站点时,这将明确该站点应该使用哪个数据库,并且如果多个站点共享数据库,您可以让它们使用不同的凭据,以便它们可以具有不同的访问限制。

      【讨论】:

        猜你喜欢
        • 2011-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-12
        • 1970-01-01
        • 2011-08-18
        • 1970-01-01
        相关资源
        最近更新 更多