繁体   English   中英

如何在C#中访问web.config连接字符串?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM