[英]How to access web.config connection string in C#?
我有一個運行VS 2008的32位XP,並且試圖從C#ASPX文件中的web.config文件解密連接字符串。
即使沒有返回錯誤,我當前的連接字符串也不顯示所選AdventureWorks存儲過程的內容。
我輸入了:
C:\Program Files\Microsoft Visual Studio 9.0\VC>Aspnet_regiis.exe -pe "connectionStrings" -app "/AddFileToSQL2"
然后它說“成功”。
我的web.config部分如下所示:
<connectionStrings>
<add name="Master" connectionString="server=MSSQLSERVER;database=Master; Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
<add name="AdventureWorksConnectionString" connectionString="Data Source=SIDEKICK;Initial Catalog=AdventureWorks;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="AdventureWorksConnectionString2" connectionString="Data Source=SIDEKICK;Initial Catalog=AdventureWorks;Persist Security Info=true; "
providerName="System.Data.SqlClient" />
</connectionStrings>
我后面的C#代碼如下所示:
string connString = ConfigurationManager.ConnectionStrings["AdventureWorksConnectionString2"].ConnectionString;
文件后面的web.config或C#代碼中的連接字符串是否有問題?
我在后面的C#代碼中設置了一個斷點,現在出現以下異常:
System.Data.SqlClient.SqlException was caught
Message="Login failed for user ''."
Source=".Net SqlClient Data Provider"
ErrorCode=-2146232060
Class=14
LineNumber=65536
Number=18456
Procedure=""
Server="SIDEKICK"
State=1
StackTrace:
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
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.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, 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 ADONET_namespace.ADONET_methods.DisplaySchemaTables() in C:\Documents and Settings\Admin\My Documents\Visual Studio 2008\Projects\AddFileToSQL2\AddFileToSQL\Admins\ADONET methods.cs:line 65
InnerException:
另外,我添加了LoginView Web控件來保護我的網站。 登錄名是“ tester”。
您正在使用的連接字符串是這樣的:
Data Source=SIDEKICK;Initial Catalog=AdventureWorks;Persist Security Info=true;
錯了 您沒有Integrated Security=True
,這意味着它將不使用Windows身份驗證。 而且您也沒有定義User Name
/ Password
,因此它不會使用任何SQL Server登錄名。
因此,您的連接字符串試圖在沒有任何憑據的情況下登錄,這就是為什么您收到該錯誤消息的原因。
要解決此問題,您需要放回Integrated Security=True
(以使用當前的Windows用戶身份),或者需要輸入特定的用戶名和密碼。
此外,在閱讀您的評論時,請注意未加密的連接字符串與通過明文發送密碼之間的區別:
當您在web.config
文件中存儲憑據信息(例如密碼)時, 加密的連接字符串很有用。 如果有人設法使用web.config
,則看不到密碼。
但是,即使您加密了連接字符串,如果連接字符串具有用戶名和密碼,該信息也會以明文形式在Web服務器和SQL Server之間發送。 但是,無論您是否加密連接字符串或web.config,使用Integrated Security
都不會通過明文發送任何憑據。 這就是使用它的原因; 集成安全性意味着將使用已登錄的Windows帳戶對SQL Server進行身份驗證。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.