将mywebsite部署到服务器后,我可以加密web.config文件吗?

时间:2015-01-28 16:57:52

标签: encryption msdeploy


我在尝试自动部署后加密web.config文件。

根据链接:https://blogs.iis.net/msdeploy/archive/2013/07/09/webdeploy-3-5-rtw.aspx

我正在使用以下命令:
msdeploy.exe -verb:sync -source:iisapp =“sourceTestSite”-dest:iisapp =“destinationTestSite”-EnableRule:EncryptWebConfig

然后我收到错误:
错误代码:ERROR_FAILED_TO_ENCRYPT_WEB_CONFIG

我不想先加密然后部署。我正在考虑运行部署脚本,在部署之后,它应该使用MSDEploy命令自动加密。

我尝试了下面的线程,但没有得到任何帮助:
Failed to encrypt destination web.config when using MS build plugin in Jenkins

此外,我想将我的秘密文件保存在不同的位置,但我发现加密过程不适用于那个 How to encrypt a file linked to a web.config


这次我尝试在远程服务器上运行命令,使用下面的代码加密web.config文件。我在我的机器中的代码下运行并尝试加密myRemoteServer上的web.config文件。

$currentDirectory = (Get-Location)
$user = "domain1\username1"
$section = "appSettings" 
$app= "/MyWeb"
$version="v4.0.30319"
$computername ="myRemoteServer"
$pwd = ConvertTo-SecureString -String "mysecret@11" -AsPlainText -Force

$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user,$pwd

$encryptcmd1= Set-Location "C:\windows\Microsoft.Net\Framework\$version"

$encryptCmd2 = ".\aspnet_regiis.exe -pe ""appSettings"" -app ""/MyWeb"""

$encryptCmd = "$encryptcmd1 $encryptcmd2"
try
{
invoke-command -ComputerName $computername -Credential $credential -ScriptBlock {$encryptCmd}
}
catch 
{
    Log-Message $_
}

Set-Location $currentDirectory

它不会抛出任何异常。但它无法正常工作,也无法加密该服务器上的web.config文件。 我想知道这里出错的地方。

1 个答案:

答案 0 :(得分:-1)

他的函数将加密web.config文件的各个部分。

function Encrypt-ConfigurationSection([int] $id, [string] $app, [string] $section, [string] $version){
$currentDirectory = (Get-Location)
Set-Location "C:\windows\Microsoft.Net\Framework\$version\"
.\aspnet_regiis.exe -pe $section -app $app -site $id -prov "RsaProtectedConfigurationProvider"
Set-Location $currentDirectory
}

示例电话

Encrypt-ConfigurationSection 1 ‘/WebApplication1’ ‘connectionStrings’ ‘v4.0.30319’

此函数将解密web.config文件的各个部分。

function Decrypt-ConfigurationSection([int] $id, [string] $app, [string] $section, [string] $version){
$currentDirectory = (Get-Location)
Set-Location "C:\windows\Microsoft.Net\Framework\$version\"
.\aspnet_regiis.exe -pd $section -app $app -site $id
Set-Location $currentDirectory
}

示例呼叫

Decrypt-ConfigurationSection 1 ‘/WebApplication1’ ‘connectionStrings’ ‘v4.0.30319’

我从这个网站得到了帮助:

https://joshjoubert.wordpress.com/2013/03/28/encrypting-and-decrypting-sections-of-a-web-config-with-powershell/