转载 How to Encrypt connection string in web.config

转载原地址: https://chiragrdarji.wordpress.com/2008/08/11/how-to-encrypt-connection-string-in-webconfig/

The most sensitive information stored in web.config file can be the connection string. You do not want to disclose the information related to your database to all the users where the application is deployed. Every time it is not possible to have a private machine for your sites, you may need to deploy the site in shared host environment. To encrypt the connection string in above situation is advisable.

ASP.NET 2.0 provides in built functionality to encrypt few sections of web.config file. The task can be completed using Aspnet_regiis.exe. Below is the web.config file and <connectionStrings> section.

  1: <connectionStrings>
   2:   <add name="cn1"
   3:           connectionString="Server=DB SERVER;
   4:                             database=TestDatabase;
   5:                             uid=UID;
   6:                             pwd=PWD;" />
   7:  </connectionStrings>

Fig – (1) Connection string section of web.config file

To encrypt the connection string section follow the steps,

1. Go to Start -> Programm Files -> Microsoft Visual Studio 2005 -> Visual Tools
    -> Microsoft Visual Studio 2005 Command Prompt

2. Type following command,

aspnet_regiis.exe -pef “connectionStrings” C:\Projects\DemoApplication

-pef indicates that the application is built as File System website.  The second argument is the name of configuration section needs to be encrypted. Third argument is the physical path where the web.config file is located.

If you are using IIS base web site the command will be,

   aspnet_regiis.exe -pe “connectionStrings” -app “/DemoApplication”

 -pe indicates that the application is built as IIS based site. The second argument is the name of configuration section needs to be encrypted. Third argument “-app” indicates virtual directory and last argument is the name of virtual directory where application is deployed.

If everything goes well you will receive a message “Encrypting configuration section…Succeeded!”

Open your web.config file and you can see that connection string is encrypted,

 1: <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
   2:   <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
   3:    xmlns="http://www.w3.org/2001/04/xmlenc#">
   4:    <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
   5:    <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
   6:     <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
   7:      <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
   8:      <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
   9:       <KeyName>Rsa Key</KeyName>
  10:      </KeyInfo>
  11:      <CipherData>
  12:       <CipherValue>Ik+l105qm6WIIQgS9LsnF8RRxQtj2ChEwq7DbHapb440GynFEoGF6Y3EM3Iw/lyDV8+P8bIsketi5Ofy9gpZlCBir7n315Q6RPbdclUo79o/LKadhX4jHFpnSIQNIF/LhwjwkLFC0=</CipherValue>
  13:      </CipherData>
  14:     </EncryptedKey>
  15:    </KeyInfo>
  16:    <CipherData>
  17:     <CipherValue>JsLrQ5S8Pq3U72nQzmSl/XlLX72GM0O3EbPLaHRNvjTDgG9seDflGMjTfO10M1s7/mPh//3MhA7pr0dNHUJ143Svhu5YXODRC6z9CkR0uyE4H7uDvTKJ8eR3m9APhXoo1sT1K3tCLHD6a2BM+gqSk9d8PzCfbM8Gmzmpjz1ElIaxu62b4cg9SNxp8o86O9N3fBl2mq</CipherValue>
  18:    </CipherData>
  19:   </EncryptedData>
  20:  </connectionStrings>

Fig – (2) Encrypted connection string section

You do not have to write any code to decrypt this connection string in your application, dotnet automatically decrypts it. So if you write following code you can see plaintext connection string.

1 Response.Write(ConfigurationManager.ConnectionStrings["cn1"].ConnectionString);

Now to decrypt the configuration section in web.config file use following command,

For File System Application,

aspnet_regiis.exe -pdf “connectionStrings” C:\Projects\DemoApplication

For IIS based Application

aspnet_regiis.exe -pd “connectionStrings” -app “/DemoApplication”

If you want to encrypt any nested section in web.config file like <pages> element within <system.web> you need to write full section name as shown below,

aspnet_regiis.exe -pef “system.web/Pages” C:\Projects\DemoApplication

You can encrypt all the sections of web.config file except following using the method I displayed in this article,

<processModel>
<runtime>
<mscorlib>
<startup>
<system.runtime.remoting>
<configProtectedData>
<satelliteassemblies>
<cryptographySettings>
<cryptoNameMapping>
<cryptoClasses>

To encrypt these section you needed to use Aspnet_setreg.exe tool.  For more detail about Aspnet_setreg.exe tool search Microsoft Knowledge Base article 329290, How to use the ASP.NET utility to encrypt credentials and session state connection strings.

时间: 2024-10-24 09:48:22

转载 How to Encrypt connection string in web.config的相关文章

使用Web.Config Transformation配置灵活的配置文件

发布Asp.net程序的时候,开发环境和发布环境的Web.Config往往不同,比如connectionstring等.如果常常有发布的需求,就需要常常修改web.config文件,这往往是一件非常麻烦的事情.Web.Config Transformation能够在不同的发布环境下,产生不同的web.config文件,非常方便和实用. 阅读目录: 一.Web.Config Transformation 二.一个实际的例子 三.Web.Config Transformation具体语法 一. We

ASP.NET MVC 5 - 创建连接字符串(Connection String)并使用SQL Server LocalDB

原文:ASP.NET MVC 5 - 创建连接字符串(Connection String)并使用SQL Server LocalDB 您创建的MovieDBContext类负责处理连接到数据库,并将Movie对象映射到数据库记录的任务中.你可能会问一个问题,如何指定它将连接到数据库? 实际上,确实没有指定要使用的数据库,Entity Framework将预设值使用的LocalDB. 在本节中,我们将显式地在Web.config文件中,添加应用程序的连接字符串(connection string)

[转]ASP.NET MVC 5 - 创建连接字符串(Connection String)并使用SQL Server LocalDB

您创建的MovieDBContext类负责处理连接到数据库,并将Movie对象映射到数据库记录的任务中.你可能会问一个问题,如何指定它将连接到数据库? 实际上,确实没有指定要使用的数据库,Entity Framework将预设值使用的LocalDB. 在本节中,我们将显式地在Web.config文件中,添加应用程序的连接字符串(connection string). SQL Server Express LocalDB LocalDB的是一个SQL Server Express轻量级版本的数据库

Entity Framework Connection String不保留密码的方法

添加Entity Data Model的时候,到最后一步,有两个radio box: 如果选择include sensitive data,虽然很方便,但是在web.config或者app.config文件的数据库链接字符串就会保留数据库的登陆密码. 如果选择不保留敏感信息,那么数据库连接字符串就会不保留密码,而我们需要在代码里面增加相关的信息. 举例子:如果创建的一个Entity Data Model名为ContactsEntities,那么,我们需要修改ContactsEntities类的构

不支持的关键字:“provider connection string”报错信息及解决方案

今天在部署公司开发框架的时候 ,登录系统之后调用代办列表的时候就报错了 总线调用契约XX.Service.Contracts.IXXService上的GetXXCount方法时出错. Resolution of the dependency failed, type = "XX.Business.Definition.IXXLogic", name = "(none)".  Exception occurred while: Calling constructor 

#error Security Issue: The connection string may contain a password

“数据链接属性”对话框的“允许保存密码”功能存在安全问题.在“输入登录服务器的信息”中有两个单选按钮:“使用 Windows NT 集成安全性” 和“使用特定的用户名和密码”. 如果选择“使用特定的用户名和密码”,则可以选择保存密码(使用“允许保存密码”复选框):但此选项不安全.建议您选择“使用 Windows NT 集成安全性”:此选项使用 Windows NT 来验证标识. 如果无法使用 Windows NT 集成安全性,则应使用中间层应用程序来提示用户输入密码,或者将密码存储在安全的位置(

web.config配置(转载)

Asp.net中的web.config配置 By Bendon 20101123 目录 Asp.net中的web.config配置... 1 一. 配置文件保存位置... 2 二. 配置文件加载顺序... 2 三. 配置文件节点介绍... 3 1. <configSections>. 3 2. <appSettings>. 5 3. <connectionStrings>. 5 4. <system.web>. 6 <location>. 11

web.config中的httpModules与httpHandlers[转载]

ASP.NET对请求处理的过程: 当请求一个*.aspx文件的时候,这个请求会被inetinfo.exe进程截获,它判断文件的后缀(aspx)之后,将这个请求转交给ASPNET_ISAPI.dll,ASPNET_ISAPI.dll会通过http管道(Http  PipeLine)将请求发送给ASPNET_WP.exe进程,在ASPNET_WP.exe进程中通过HttpRuntime来处理这个请求,处理完毕将结果返回客户端. inetinfo.exe进程:是www服务的进程,IIS服务和ASPNE

[转载]django在eclipse环境下建web网站

一.创建一个项目如果这是你第一次使用Django,那么你必须进行一些初始设置.也就是通过自动生成代码来建立一个Django项目--一个Django项目的设置集,包含了数据库配置.Django详细选项设置和应用 特性配置,具体操作步骤如下所示. 1.新建Django项目选择sqlite数据库 2.创建网站模块app 3.测试新建的模块是否正常 Validating models... 0 errors found March 12, 2014 - 10:26:53 Django version 1