簡體   English   中英

IIS SQL Server配置

[英]IIS SQL Server configuration

我有一個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

這很容易解釋-由於當前登錄的用戶不是IIS_IUSRS,我的帶有集成安全性= true的連接字符串無法成功工作。

有人可以幫助顯示我以該新IIS用戶身份連接時應更改連接字符串以創建連接的內容嗎?

您需要更改運行應用程序池的用戶。

轉到“應用程序池的高級設置”,然后將標識更改為可以訪問sql服務器的用戶。

在此處輸入圖片說明

實際上,您可以使用該連接字符串,只需向SQL Server和數據庫中的IIS APPPOOL\\MYDB授予權限。

從長遠來看,使用站點的顯式帳戶可能使管理安全性更加容易,例如,將連接字符串放在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獲取連接字符串。

當服務器上有多個站點時,這將使該站點應該使用哪個數據庫變得清楚,如果多個站點共享數據庫,則可以使它們使用不同的憑據,以便它們可以具有不同的訪問限制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM