1.分析

由于Azure Web AppService平台的特殊性,所以在C#中原先的config加密方法DataProtectionConfigurationProviderRSAProtectedConfigurationProvider在Azure平台上面是无法使用的,会在发布一段时间后失效或者无法解密,所以推荐在Azure上采用证书的方式加密和解密config配置文件(在Azure门户中的应用设置中的应用设置和连接字符串是采用静态加密的,如果只是针对WebAPP的话推荐采用上述方式)。

2.解决方法

1.创建加密证书,使用PowerShell工具在Windows机器上创建证书,命令如下(PowerShell需要以管理员的方式运行)

$cert = New-SelfSignedCertificate -Type DocumentEncryptionCert -Subject "CN=DevConfig" -KeyExportPolicy Exportable -KeySpec KeyExchange

Export-Certificate -Cert $cert -FilePath ".\DevConfig.cer"

$mypwd = ConvertTo-SecureString -String "" -Force -AsPlainText

Export-PfxCertificate -Cert $cert -FilePath ".\DevConfig.pfx" -Password $mypwd
$cert

使用Export-Certificate命令将加密证书导出为“.cer”文件,使用Export-PfxCertificate将解密证书导出为“.pfx”文件,最后的命令是用来查看证书的指纹的。

2.将加密证书导入windows

Import-Certificate -Filepath ".\DevConfig.cer" -CertStoreLocation cert:\LocalMachine\My

3.将解密证书导入到windows

$mypwd = ConvertTo-SecureString -String "" -Force -AsPlainText

Import-PfxCertificate -FilePath ".\DevConfig.pfx" -CertStoreLocation Cert:\LocalMachine\My -Password $mypwd

4.设置用于加密的web.config文件,如果是webjob的话需要把app.config改成web.config

在nuget中下载WebConfigEncrypter这个包,添加到项目中,将下面的内容添加到webconfig中,将指纹的值改成之前生成的证书的指纹的值

<configuration>
[...]
<configProtectedData>
<providers>
<add name="Pkcs12Provider" thumbprint="1234123412341234123412341234123412341234" type="WebConfigEncrypter.Pkcs12ProtectedConfigurationProvider, WebConfigEncrypter" storeLocation="LocalMachine"/>
</providers>
</configProtectedData>

5.加密web.config中的节点(appsettings和Connection strings)

打开visual studio命令行代码(在开始菜单程序中找到vs里面就有这个命令行快捷方式),在里面输入下列代码

aspnet_regiis -pef "connectionStrings" "webconfig所在的绝对路径" -prov "Pkcs12Provider"

如果执行命令出错,运行where aspnet_regiis命令,将下面的这三个dll放到运行命令后出来的文件夹中。

  • WebConfigEncrypter.dll
  • System.Configuration.ConfigurationManager.dll
  • System.Security.Cryptography.Xml.dll

加密后的web.config如下图所示

<connectionStrings configProtectionProvider="Pkcs12Provider">
<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#aes192-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>rsaKey</KeyName>
</KeyInfo>
<CipherData>
<CipherValue>Moy/a2XO2zvnn/HZW53DyC8aAJWo16+0KmnpC4SCSmuQZU0RT+HNFEA33pAGCzve+m6MTaRzhx6jVVRoAvpSNzfYG1bU1z7a1YnbW4OGxrmYYfdWW6cZQZ57dZnL6YSAlkJ5WlqPDGUPJa6FV/hTic3x4fJYy5vdSucmO6X3opuo1998LWNkL6fIS4WkjkG/SOFbI2Qx3HHogdN670jDHKNDON1z7bFHhLNyVj7RTO3xuQN9kF4PqbFtvwm1bYXTbZpdNxu/fcXZKONSAu8HN3QX5vTRyP/I4BG+NK7TUig3gxD4tq9GR7aSSGKJyt02PiCEO0JpyyIbHZ9xbck9kw==</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>TeV0yJaFlEhpyZUlQoG7M3O7sfQ7uG3ndgmhxipOrwoEsrI+Zvt1NI7arefOFWGNW4CEaoLo4mKy2Kwr4ZgK+6rAwOmx1IRyheWtF7z/8+CiGOqSRXLyGEkDQBEVOWKU0Y6TaWtPu0ZM3bp5pvKaztBnthgGnrGYmigaufu5rZW1GWPtHyL2iWdAkU9iaf+AOpA/GSvoVtZmnfJ1rwy6U8PTO0h0Ws/PdkcOKuXGkx31t/Y32ivFoy7xYPnPt/Z/aNMiHvbO7faQAwuJ/NsG9G1FFRRHCqc73TUsRdKHVuf17BEp526RG6RBZtM3F3V3o0d8/sLmyrNI9tFfksB4qcWiN4P+BRtGr0iacmBfBOvAFSozfUYxjMpx+BYPOpD1pf4fMFoKxxKeJYY31XqZoQLp75RgmWhWYm8URHq4Cjs=</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>

6.将加密成功后的应用发布到Azure上,发布之前将web.config中配置加密的节点改成如下,将storeLocation的值变为当前用户

<add name=“Pkcs12Provider” thumbprint=“1234123412341234123412341234123412341234" type=“WebConfigEncrypter.Pkcs12ProtectedConfigurationProvider, WebConfigEncrypter” storeLocation=“CurrentUser”/>

7.在Azur中上传刚才生成好的pfx证书

8.在Azure的应用设置中加入key为WEBSITE_LOAD_CERTIFICATES,value为*的键值对,让webapp可以读取应用的证书。

9.测试网站是否可以正常运行。

Azure web site和web job的config文件加密方式的更多相关文章

  1. Windows Azure Web Site (10) Web Site测试环境

    <Windows Azure Platform 系列文章目录> 我们知道,在使用Azure Cloud Service的时候,会有2个不同的环境,称为Production环境和Stagin ...

  2. Windows Azure Web Site (7) Web Site配置

    <Windows Azure Platform 系列文章目录> 在上一章内容中,我们已经部署了Azure WebSite.我们可以在Web Site配置页面进行配置.如下图: 另外,我们还 ...

  3. Windows Azure Web Site (9) Web Site公网IP地址

    <Windows Azure Platform 系列文章目录> 本文会同时介绍国内由世纪互联运维的Azure China和海外Azure Global. 熟悉Windows Azure平台 ...

  4. .net web site 和 web application 的区别

    web application 会把所有的代码编译打包成单一的库文件(.dll). web site 不会对整个的代码进行编译,在运行时须要哪一段代码就编译哪段代码.这导致web site 上线后,如 ...

  5. Microsoft Azure Web Sites应用与实践【2】—— 通过本地IIS 远程管理Microsoft Azure Web Site

    Microsoft Azure Web Sites应用与实践 系列: [1]—— 打造你的第一个Microsoft Azure Website [2]—— 通过本地IIS 远程管理Microsoft ...

  6. Windows Azure Web Site (12) Azure Web Site配置文件

    <Windows Azure Platform 系列文章目录>  本文将介绍如何在Azure Web Site里配置连接字符串. 本文分为以下几个步骤: 1.在本地ASP.NET项目使用W ...

  7. Windows Azure Web Site (14) Azure Web Site IP白名单

    <Windows Azure Platform 系列文章目录> 我们知道,在Azure Cloud Service和Virtual Machine,可以通过Endpoint ACL (Ac ...

  8. Windows Azure Web Site (15) 取消Azure Web Site默认的IIS ARR

    <Windows Azure Platform 系列文章目录> 我们知道,Azure Web Site (改名为Azure Web App)默认是可以保留Session的.Azure We ...

  9. Windows Azure Web Site (16) Azure Web Site HTTPS

    <Windows Azure Platform 系列文章目录> 我们在使用微软云Azure Web App的时候,会使用微软的二级域名:http://xxx.chinacloudsites ...

随机推荐

  1. java.sql.SQLException: Incorrect string value: '\xE5\xB0‘

    mysql插入中文字符报java.sql.SQLException: Incorrect string value: '\xE5\xB0‘ #原因:由于默认情况下,mysql的字符集是latin1(I ...

  2. 一些需要禁用的PHP危险函数

    phpinfo()功能描述:输出 PHP 环境信息以及相关的模块.WEB 环境等信息.危险等级:中 passthru()功能描述:允许执行一个外部程序并回显输出,类似于 exec().危险等级:高 e ...

  3. 【Java123】JDBC数据库连接池建立

    需求场景:多SQL任务多线程并行执行 解决方案:建立JDBC数据库连接池,将线程与连接一对一绑定 https://www.cnblogs.com/panxuejun/p/5920845.html ht ...

  4. print输出

    python中的print输出可以用逗号"," >>> a = 1 >>> b = 2 >>> print a,b 1 2 逗 ...

  5. ls: Call From hdoop2/192.168.18.87 to hdoop2:8020 failed on connection exception: java.net.ConnectException: 拒绝连接; For more details see

    场景:  预发环境中,同事已经搭建了一套hadoop集群,由于版本与所需不符,所以需要替换版本 问题描述: 在配置文件都准确的情况下,启动hadoop,出现以下报错: 启动之前初始化:   初始化目录 ...

  6. 在文章详情页调用seo标题标签

    在新闻的详情页,调用当前栏目的seo标题,原标签是:{dede:field.seotitle /} {dede:type} [field:seotitle/] {/dede:type} 修改 \inc ...

  7. Altium Ddesigner 栅格 含义

    栅格分为可视栅格(Visible Grid).捕获栅格(snap grid).元件放置捕获栅格(Component Grid).电气栅格(Electrical Grid). 可视栅格:就是编辑过程中看 ...

  8. css笔记--用户界面样式

    1.系统字体,系统颜色.根据关键字设置为系统某方面相同的字体和颜色 2.光标:鼠标移入时光标的不同显示方法,有十字键,手型:cursor:pointer;cursor:hand;要按顺序,cursor ...

  9. H5 开发中常见的小问题

    1.解决 浏览器 返回按钮不刷新的问题 window.onpageshow = function(event) { if (event.persisted) { window.location.rel ...

  10. Linux磁盘与文件系统管理(一)

    fdisk 常用的磁盘分区工具,受mbr分区表的限制,只能给小于2TB的磁盘划分分区,如果使用fdisk对大于2TB的磁盘进行分区,虽然可以分区,但只能识别2T的空间,一般使用parted分区工具 - ...