【问题标题】:After Deploying mywebsite to server can I encrypt the web.config file?将 mywebsite 部署到服务器后,我可以加密 web.config 文件吗?
【发布时间】:2015-01-28 16:57:51
【问题描述】:


我正在尝试在自动部署后加密 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 文件。 我想知道这里哪里/哪里出了问题。

【问题讨论】:

  • 目前我正在做下面的事情来使用powershell进行加密

标签: encryption msdeploy


【解决方案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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 2021-04-18
    • 1970-01-01
    • 2016-08-20
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多