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. SERVICE问题解决方法

    这篇文章主要介绍了Windows服务器下出现ZendOptimizer.MemoryBase@NETWORK SERVICE问题解决方法,需要的朋友可以参考下 日志提示 事件 ID ( 2 )的描述( ...

  2. 700. Search in a Binary Search Tree

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  3. PAT乙级1022

    1022 D进制的A+B (20 分)   输入两个非负 10 进制整数 A 和 B (≤2​30​​−1),输出 A+B 的 D (1<D≤10)进制数. 输入格式: 输入在一行中依次给出 3 ...

  4. Sublime Text常用设置之个人配置

    一.安装 1.安装包下载:  http://www.sublimetext.com/3 (傻瓜式安装) 2.Package Control安装: 1)Ctrl+~或者View——Show Consol ...

  5. POJ 2250 (LCS,经典输出LCS序列 dfs)

    题目链接: http://poj.org/problem?id=2250 Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  6. iOS开发用如何用类"SKStoreProductViewController"跳转AppStore点赞评分?

    大家都知道,评论和评分是决定app在appstore中排名的重要因素,但是大部分用户下载安装APP后却不会去点评,所以添加提示用户去点评的功能是很必要的. 目前,AppStore点赞评分有两种方法,一 ...

  7. 文本处理三剑客之 awk

    GAWK:报告生成器,格式化文本输出 awk [options] ‘program’ var=value file… awk [options] -f programfile var=value fi ...

  8. redis五种数据结构及使用场景

    string(字符串) 存储最简单的key-value结构. value可以是字符串.整数或者浮点数. 可以对整个字符串或者字符串的一部分执行操作: 对整数和浮点数执行自增或者自减操作. 使用场景: ...

  9. mac 下安装php7.1 memcache扩展

    1.下载memcache源代码文件 https://github.com/websupport-sk/pecl-memcache/archive/php7.zip 文件夹名为:pecl-memcach ...

  10. QT中QToolTip样式设置的两种方式

    方式一 使用样式表设置 ui.label->setStyleSheet("QToolTip{border:1px solid rgb(118, 118, 118); backgroun ...