https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-3.0/dx0f3cf2(v=vs.85)

When working with data source controls it is recommended that you centralize the location of your connection strings by storing them in the application's Web.config file. This simplifies the management of connection strings by making them available to all of the ASP.NET pages in a Web application. In addition, you do not need to modify numerous individual pages if your connection string information changes. Finally, you can improve the security of sensitive information stored in a connection string, such as the database name, user name, password, and so on, by encrypting the connection string section of the Web.config file using protected configuration.

This topic describes how to store connection strings in the connectionStrings configuration section in the Web.config file, and how to use the command-line .NET Framework tool to encrypt connection strings for additional security.

To store a connection string in the Web.config file

  1. Open the Web.config file for your application. If a Web.config file does not already exist, create a text file named Web.config and add the following content:

    Copy
    <?xml version="1.0"?>
    <configuration
    xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
    <appSettings/>
    <system.web>
    </system.web>
    </configuration>
  2. In the configuration element, create a new element named connectionStrings, as shown in the following example:

    Copy
    <?xml version="1.0"?>
    <configuration
    xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
    <connectionStrings> </connectionStrings>
    <appSettings/>
    <system.web>
    </system.web>
    </configuration>
  3. In the connectionStrings element, create an add element for each connection string you will use in your Web application. Include the attributes shown in the following table.

    Attribute Description
    1. name

    A name for this connection string configuration object. This name will be used by data source controls and other features to reference the connection string information.

    1. connectionString

    The connection string to the data source.

    1. providerName

    The namespace of the NET Framework data provider to use for this connection, such as System.Data.SqlClientSystem.Data.OleDb or System.Data.Odbc.

    A completed connectionStrings element might look like the following example:

    Copy
    <connectionStrings>
    <add
    name="NorthwindConnection"
    connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;" />
    </connectionStrings>
  4. Save and close the Web.config file.

    You can now reference the connection string for your data source control by referring to the name you specified for the name attribute.

  5. In the ConnectionString attribute for your data source control, use the connection string expression syntax to reference the connection information from the Web.config file.

    The following example shows a SqlDataSource control in which the connection string is read from the Web.config file:

    Copy
    <asp:SqlDataSource ID="ProductsDataSource" Runat="server"
    SelectCommand="SELECT * from Products"
    ConnectionString="<%$ ConnectionStrings: NorthwindConnection %>"
    </asp:SqlDataSource>

To encrypt connection string information stored in the Web.config file

  1. At the Windows command line, run the ASP.NET IIS registration tool (aspnet_regiis.exe) with the following options:

    • The -pe option, passing it the string "connectionStrings" to encrypt the connectionStrings element.

    • The -app option, passing it the name of your application.

    The aspnet_regiis.exe tool is located in the %systemroot%\Microsoft.NET\Framework\versionNumber folder.

    The following example shows how to encrypt the connectionStrings section of the Web.config file for an application named SampleApplication:

    Copy
    aspnet_regiis -pe "connectionStrings" -app "/SampleApplication"

    When the command has finished, you can view the contents of the Web.config file. The connectionStringsconfiguration section will contain encrypted information instead of a clear-text connection string, as shown in the following example:

    Copy
    <configuration>
    <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
    xmlns="http://www.w3.org/2001/04/xmlenc#">
    <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
    <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
    <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
    <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
    <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
    <KeyName>RSA Key
    </KeyName>
    </KeyInfo>
    <CipherData>
    <CipherValue>WcFEbDX8VyLfAsVK8g6hZVAG1674ZFc1kWH0BoazgOwdBfinhcAmQmnIn0oHtZ5tO2EXGl+dyh10giEmO9NemH4YZk+iMIln+ItcEay9CGWMXSen9UQLpcQHQqMJErZiPK4qPZaRWwqckLqriCl9X8x9OE7jKIsO2Ibapwj+1Jo=
    </CipherValue>
    </CipherData>
    </EncryptedKey>
    </KeyInfo>
    <CipherData>
    <CipherValue>OpWQgQbq2wBZEGYAeV8WF82yz6q5WNFIj3rcuQ8gT0MP97aO9SHIZWwNggSEi2Ywi4oMaHX9p0NaJXG76aoMR9L/WasAxEwzQz3fexFgFSrGPful/5txSPTAGcqUb1PEBVlB9CA71UXIGVCPTiwF7zYDu8sSHhWa0fNXqVHHdLQYy1DfhXS3cO61vW5e/KYmKOGA4mjqT0VZaXgb9tVeGBDhjPh5ZlrLMNfYSozeJ+m2Lsm7hnF6VvFm3fFMXa6+h0JTHeCXBdmzg/vQb0u3oejSGzB4ly+V9O0T4Yxkwn9KVDW58PHOeRT2//3iZfJfWV2NZ4e6vj4Byjf81o3JVNgRjmm9hr9blVbbT3Q8/j5zJ+TElCn6zPHvnuB70iG2KPJXqAj2GBzBk6cHq+WNebOQNWIb7dTPumuZK0yW1XDZ5gkfBuqgn8hmosTE7mCvieP9rgATf6qgLgdA6zYyVV6WDjo1qbCV807lczxa3bF5KzKaVUSq5FS1SpdZKAE6/kkr0Ps++CE=
    </CipherValue>
    </CipherData>
    </EncryptedData>
    </connectionStrings>
    </configuration>

    Leave the command prompt open for later steps.

  2. Determine the user account or identity under which ASP.NET runs by retrieving the current WindowsIdentity name.

    The following example shows one way to determine the WindowsIdentity name:

    C#Copy
    <%@ Page Language="C#" %>
    <%
    Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
    %>
    Note

    By default, on Windows Server 2003 with impersonation for an ASP.NET application disabled in the Web.config file, the identity under which the application runs is the NETWORK SERVICE account. On other versions of Windows, ASP.NET runs under the local ASPNET account.

    The user account or identity under which ASP.NET runs must have read access to the encryption key used to encrypt and decrypt sections of the Web.config file. This procedure assumes that your Web site is configured with the default RsaProtectedConfigurationProvider specified in the Machine.config file named "RsaProtectedConfigurationProvider". The RSA key container used by the default RsaProtectedConfigurationProvider is named "NetFrameworkConfigurationKey".

  3. At the command prompt, run the aspnet_regiis.exe tool with the following options:

    • The -pa option, passing it the name of the RSA key container for the default RsaProtectedConfigurationProvider.

    • The identity of your ASP.Net application, as determined in the preceding step.

    The following example shows how to grant the NETWORK SERVICE account access to the machine-level "NetFrameworkConfigurationKey" RSA key container:

    Copy
    aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY\NETWORK SERVICE"
  4. To decrypt the encrypted Web.config file contents, run the aspnet_regiis.exe tool with the -pd option. The syntax is the same as encrypting Web.config file contents with the -pe option except that you do not specify a protected configuration provider. The appropriate provider is identified in the configProtectionProvider attribute for the protected section.

    The following example shows how to decrypt the connectionStrings element of ASP.NET application SampleApplication.

    Copy
    aspnet_regiis -pd "connectionStrings" -app "/SampleApplication"

How to: Secure Connection Strings When Using Data Source Controls的更多相关文章

  1. [转]Oracle connection strings

    本文转自:http://www.connectionstrings.com/oracle/ Standard Data Source=MyOracleDB;Integrated Security=ye ...

  2. Windows Phone本地数据库(SQLCE):9、Connection Strings(翻译) (转)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第八篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知 ...

  3. Data source rejected establishment of connection, message from server: "Too many connections"解决办法

    异常名称 //数据源拒绝从服务器建立连接.消息:"连接太多" com.MySQL.jdbc.exceptions.jdbc4.MySQLNonTransientConnection ...

  4. Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.

    Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Xcode7 beta 网络请求报错:The ...

  5. The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.问题解决

    didFailLoadWithError(): Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loa ...

  6. Data source rejected establishment of connection, message from server: &quot;Too many connections&quot;

    错误叙述性说明: 測试一段时间没有不论什么问题.今天突然用户无法登录,报错如Data source rejected establishment of connection,  message fro ...

  7. Unable to open connection to supplicant on "/data/misc/wifi/sockets/wlan0"

    在调试android wifi UI 的时候,出现了 logcat:  Unable to open connection to supplicant on "/data/misc/wifi ...

  8. Data source rejected establishment of connection, message from server: "Too many connections"

    详细错误信息: Caused by: com.MySQL.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source ...

  9. [转】[tip] localhost vs. (local) in SQL Server connection strings

    主要区别在于连接协议不同,前者(localhost)使用TCP协议,后者("(local)")使用NamedPipe协议. Sample code with SQL Server ...

随机推荐

  1. OSI标准协议分析

    1.各个层的作用 物理层:(physical Layer):物理层负责传送比特(Bit),涉及到接口和传输媒体的机械 电气特性 数据链路层:(data link layer):数据链路层负责传送的帧( ...

  2. VLAN实验3:理解Hybrid接口的应用

    实验环境 实验拓扑图 实验编址 实验步骤1.基本配置按照实验编址为PC配置IP地址,以PC5为例 在PC5与PC1通过ping命令测试,发现通讯正常.(以此为例,其他的我就不一一截图测试了.) 在S1 ...

  3. 使用Cloudera Manager部署oozie

    使用Cloudera Manager部署oozie 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 1>.进入CM服务安装向导 2>.选择要添加的oozie服务 3> ...

  4. plsql查看是否锁表,锁模式等,以及解锁SQL

    --工作中的点滴积累SELECT l.session_id sid, s.serial#, l.locked_mode 锁模式, l.oracle_username 登录用户, l.os_user_n ...

  5. 自定义Lombok注解

    Java 是一门"繁琐"的语言,使用 Lombok 可以显著地减少样板代码.比如使用 @Getter 注解可以为你的私有属性创建 get 方法. 源代码 @Getter priva ...

  6. Andrew Ng机器学习 五:Regularized Linear Regression and Bias v.s. Variance

    背景:实现一个线性回归模型,根据这个模型去预测一个水库的水位变化而流出的水量. 加载数据集ex5.data1后,数据集分为三部分: 1,训练集(training set)X与y: 2,交叉验证集(cr ...

  7. Linux系统下文件压缩与打包命令

    Linux系统下文件压缩与打包命令 常用的压缩文件拓展名 * .Z * .zip * .gz * .bz2 * .xz * .tar * .tar.gz * .tar.bz2 * .tar.xz 压缩 ...

  8. Oracle 序号函数

    Oracle提供的序号函数:以emp表为例:1: rownum 最简单的序号 但是在order by之前就确定值.select rownum,t.* from emp t order by ename ...

  9. php 实现密码错误三次锁定账号10分钟

    /** * 登录 * 1.接收数据 * 2.正则判断接收到的数据是否合理 * 3.根据用户名获取用户数据 * 获取到数据 -> 继续执行 * 没有获取到数据 -> 提示:用户名密码错误 * ...

  10. Go语言 - 数组 | 多维数组

    Array 数组是同一种数据类型元素的集合. 在Go语言中,数组从声明时就确定,使用时可以修改数组成员,但是数组大小不可变化. 1.数组 在定义阶段,长度和类型就固定了,以后不能更改 2.长度也是数组 ...