[英]Web service with database : SqlException
我寫了一個 web 服務,它與 SQL Server'08 數據庫一起使用
當我嘗試調用 web 方法時,我得到了這個:
System.Data.SqlClient.SqlException: Login failed for user 'IIS APPPOOL\ASP.NET v4.0'.
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.Linq.SqlClient.SqlConnectionManager.UseConnection(IConnectionUser user)
at System.Data.Linq.SqlClient.SqlProvider.get_IsSqlCe()
at System.Data.Linq.SqlClient.SqlProvider.InitializeProviderMode()
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.DataQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at route.Logic..ctor() in C:\Users\Ilya\Documents\Visual Studio 2010\Projects\MobitourLibrary\route\Logic.cs:line 20
at Service..ctor() in c:\inetpub\wwwroot\RouteGen\App_Code\Service.cs:line 14
問題出在哪里? 其他 win forms 應用程序使用相同的 DB 工作正常,為什么 WS 沒有?
web 服務將在與 WinForms 應用程序不同的用戶帳戶下運行。 WinForms 應用程序將從登錄用戶的用戶帳戶運行,而 web 服務將是 ASP.NET 使用的帳戶。
您需要在 SQL 服務器中設置該帳戶並為其授予相關權限。
基本上,您需要將登錄名添加到服務器,並將該登錄名作為用戶添加到需要它的每個數據庫中。
以下是您在 SQL Server Management Studio 中時的分步說明:
您現在有一個新的登錄名。
現在,在數據庫中創建用戶。
現在應該一切都好。
或者
您可以更改 web 服務中的連接字符串,以使其使用特定帳戶連接到 SQL 服務器。
Colin Mackay
為您的問題提供了詳細的答案。 這是我的兩分錢。
如果您的環境中有 Active Directory 域設置,我不建議您在連接中包含user id
和password
。 相反,我會建議以下內容:
創建一個域帳戶MyDomain\webservice
,其中MyDomain將是活動目錄域,而webservice將是該域中的 Windows 用戶帳戶。 在SQL
中,為該新域帳戶用戶提供訪問數據庫的適當權限。
在Internet Information Services (IIS) Manager
中,將應用程序池更改為在此服務帳戶下運行。 如果您正在運行 IIS 7.5,則可以執行以下步驟:
在IIS
中,展開<server name>
節點並單擊Application Pools
。
最好創建一個新的Application Pool
,這樣您就不會干擾可能使用應用程序池ASP.NET v4.0
的其他應用程序的功能。
創建類似於 ASP.NET v4.0 應用程序池的新應用程序池(例如WebServiceAppPool
),除了新應用程序池將使用新創建的域帳戶MyDomain\webservice
的身份,而不是通常的ApplicationPoolIdentity 。
在 web 服務部署到的虛擬目錄/站點的Advanced Settings
選項中,更改應用程序池屬性以使用新創建的應用程序池WebServiceAppPool
。
我相信這種設置更安全,並且可以避免在連接字符串中硬編碼用戶 ID 和密碼。
希望有幫助。
該消息清楚地說明了它: LOGIN FAILED .....用戶( IIS APPPOOL\ASP.NET v4.0
試圖連接到數據庫沒有權限。
不知道任何其他細節,我假設這是一個 ASP.NET web 應用程序/Web 服務托管在 IIS 中,並且您很可能打開了與數據庫的連接字符串 - 因此,當前的用戶Integrated Security=SSPI
設置已這里 IIS apppool 用戶)嘗試連接到數據庫 - 但不能。
因此,更改您的連接字符串(請參閱http://www.connectionstrings.com上的大量示例)以指定可以連接的特定數據庫用戶:
server=YourServer;database=YourDatabase;User ID=SomeValidUser;Pwd=Top$ecret
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.