转载 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.
: <connectionStrings>
: <add name="cn1"
: connectionString="Server=DB SERVER;
: database=TestDatabase;
: uid=UID;
: pwd=PWD;" />
: </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,
: <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>Ik+l105qm6WIIQgS9LsnF8RRxQtj2ChEwq7DbHapb440GynFEoGF6Y3EM3Iw/lyDV8+P8bIsketi5Ofy9gpZlCBir7n315Q6RPbdclUo79o/LKadhX4jHFpnSIQNIF/LhwjwkLFC0=</CipherValue>
: </CipherData>
: </EncryptedKey>
: </KeyInfo>
: <CipherData>
: <CipherValue>JsLrQ5S8Pq3U72nQzmSl/XlLX72GM0O3EbPLaHRNvjTDgG9seDflGMjTfO10M1s7/mPh//3MhA7pr0dNHUJ143Svhu5YXODRC6z9CkR0uyE4H7uDvTKJ8eR3m9APhXoo1sT1K3tCLHD6a2BM+gqSk9d8PzCfbM8Gmzmpjz1ElIaxu62b4cg9SNxp8o86O9N3fBl2mq</CipherValue>
: </CipherData>
: </EncryptedData>
: </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.
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.
转载 How to Encrypt connection string in web.config的更多相关文章
- 【转】Encrypt ConnectionString in Web.Config 【加密ASP.NET web.config数据库链接字串】
原文链接:https://www.codeproject.com/Tips/795135/Encrypt-ConnectionString-in-Web-Config web.config中一般会存放 ...
- 使用Web.Config Transformation配置灵活的配置文件
发布Asp.net程序的时候,开发环境和发布环境的Web.Config往往不同,比如connectionstring等.如果常常有发布的需求,就需要常常修改web.config文件,这往往是一件非常麻 ...
- Web.Config Transformation配置灵活的配置文件
使用Web.Config Transformation配置灵活的配置文件 发布Asp.net程序的时候,开发环境和发布环境的Web.Config往往不同,比如connectionstring等.如果常 ...
- 关于Web.config的debug和release.config文件
使用Web.Config Transformation配置灵活的配置文件 发布Asp.net程序的时候,开发环境和发布环境的Web.Config往往不同,比如connectionstring等.如果常 ...
- 如何用代码方式获取Web.config中system.serviceModel/client节点的address
以下代码GetAPIAddress将返回:http://localhost:2555/APITEST.asmx using System.Web.Configuration;using System. ...
- MVC: Connection String
背景: 之前项目使用的是DB first/Model first,现在要对EF升级的6.0,并且更换成Code first. 问题: 1. System.Data.Entity.Core.Metada ...
- ASP.NET MVC 5 - 创建连接字符串(Connection String)并使用SQL Server LocalDB
您创建的MovieDBContext类负责处理连接到数据库,并将Movie对象映射到数据库记录的任务中.你可能会问一个问题,如何指定它将连接到数据库? 实际上,确实没有指定要使用的数据库,Entity ...
- No connection string named '***' could be found in the application config file
Code-First时更新数据库遇到妖孽问题“No connection string named '***' could be found in the application config fil ...
- EF 数据库连接约定(Connection String Conventions in Code First)
一个典型的EF应用大多数情况下是一个DbContext的派生类(derived class)来控制,通常可以使用该派生类调用DbContext的构造函数,来控制以下的东西: (1).上下文如何连接到数 ...
随机推荐
- [itint5]区间相交
http://www.itint5.com/oj/#14 要记录原来的索引,所以用了额外的空间,新生成一个结构.如果要省空间,可以用指针来排序,最后拿指针减去索引0的位置就是index,见:http: ...
- C#+SQL数据库备份和还原
使用前要导入SQLDMO.dll(在com组件中导入Microsoft SQLDMO Object Library即可) /// /// DbOper类,主要应用SQLDMO实现对Microsoft ...
- Django用户认证系统(二)Web请求中的认证
在每个Web请求中都提供一个 request.user 属性来表示当前用户.如果当前用户未登录,则该属性为AnonymousUser的一个实例,反之,则是一个User实例. 你可以通过is_authe ...
- 泛型编程、STL的概念、STL模板思想及其六大组件的关系,以及泛型编程(GP)、STL、面向对象编程(OOP)、C++之间的关系
2013-08-11 10:46:39 介绍STL模板的书,有两本比较经典: 一本是<Generic Programming and the STL>,中文翻译为<泛型编程与STL& ...
- 1106. Two Teams(dfs 染色)
1106 结点染色 当前结点染为黑 朋友染为白 依次染下去 这题是为二分图打基础吧 #include <iostream> #include<cstdio> #include ...
- java 死锁及解决
Java线程死锁如何避免这一悲剧 Java线程死锁需要如何解决,这个问题一直在我们不断的使用中需要只有不断的关键.不幸的是,使用上锁会带来其他问题.让我们来看一些常见问题以及相应的解决方法: Jav ...
- poj2392
首先按限制高度排序,然后按多重背包做dp 这里的背包只用知道每种状态是否可行,所以 这里的多重背包可以变成O(nm) ; ..,..,..] of longint; a,b:..] of lo ...
- 数论/the first wave
线性筛素数(原来我之前学的不是线性的啊... void getprime(){ rep(i,2,nmax){ if(!vis[i]) prime[++prime[0]]=i; for(int j=1; ...
- poj2014 不带修改区间第k大树
主席树 又称函数式线段树,又称可持久化线段树……缺点是内存有点儿大…… type node1=record l,r,sum:longint; end; node2=record x,idx:longi ...
- 陈正冲老师讲c语言void关键字
1. void a void的字面意思是“空类型”,void *则为“空类型指针”,void *可以指向任何类型的数据. void几乎只有“注释”和限制程序的作用,因为从来没有人会定义一个void变量 ...