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. oracle 禁用/启动job

    注意:dbms_job只能在job的所在用户使用,如果broken其它用户的job用dbms_ijob dbms_job只能在当期用户内创建job.修改和删除job,不能对其他用户的job进行操作;s ...

  2. virtualbox+vagrant学习-3-Vagrant Share-4-Vagrant Connect

    Vagrant Connect vagrant可以共享到vagrant环境的任何或每个端口,而不仅仅是SSH和HTTP.“vagrant connect”命令为连接人员提供一个静态IP,他们可以使用该 ...

  3. windows 下nginx配置php支持

    修改nginx配置 location ~ \.php$ { root D:/Learn/php/test/; fastcgi_pass ; fastcgi_index index.php; fastc ...

  4. 展开label,利用YYText实现文字显示不完末尾添加全文

    效果图: 操作 先github下载<YYText>文件导入, 代码如下: #import "ViewController.h" #import "YYLabe ...

  5. go lang中局部变量的内存分配

    晚上在阅读go lang的资料时突然想到一个问题,go是如何分配变量的内存结构的呢?好在网上的一篇文章做了透彻的分析见[go语言局部变量分配在栈还是堆]. 其结论是go语言局部变量的分配是由编译器决定 ...

  6. Ubuntu 16.04 下简单安装使用golang之备忘

      刚开始学习Go语言,这里记录下我在Ubuntu 16.04下安装使用golang的过程,方便以后查询.   一.安装   1.添加源   如果使用默认的源安装golang的话,版本太低,只到1.6 ...

  7. 基于 Keras 用深度学习预测时间序列

    目录 基于 Keras 用深度学习预测时间序列 问题描述 多层感知机回归 多层感知机回归结合"窗口法" 改进方向 扩展阅读 本文主要参考了 Jason Brownlee 的博文 T ...

  8. WP10的一点小问题

    兼容WP8.0/WP7.5不太完整!也许是测试版的问题.毕竟还没发布正式版! 具体如:WP8.0或WP7.5的启动器!就是选择图片的启动器!调用后对话框返回的结果都是Cancel本来应该是OK的.也就 ...

  9. c++ 字符串和字符串库函数

    //有2个字符串定义如下: //char s1[38]="www.yjxsoft"; //char s2[]=".com"; //请编写一段代码,把字符串s2追 ...

  10. PyQt5 笔记(04):主窗口卡死问题

    本文基于:windows 7 + python 3.4 知识点: 1. 将 time.sleep 替换为 QTimer 2. 将 time.sleep 放入到 QThread 3. 使用 QThrea ...