C# IIS通过命令行为Jenkins生成机器密钥

C# IIS通过命令行为Jenkins生成机器密钥,c#,asp.net,iis,jenkins,C#,Asp.net,Iis,Jenkins,我们有一个负载平衡的环境,Jenkins会自动部署提交。因为我们使用信号器来工作,所以我们必须在服务器场中的所有服务器中使用相同的机器密钥。我们可以手动进入IIS管理器并生成将包含在web.config文件中的密钥。但是因为我们使用Jenkins进行自动部署,所以我们也需要自动部署。我试图通过命令行找到一种方法,但找不到任何资源。是否有方法生成机器密钥并将其添加到web.config,以便所有过程都是自动的 您可以使用powershell通过命令行生成机器密钥。Microsoft提供此代码,可在

我们有一个负载平衡的环境,Jenkins会自动部署提交。因为我们使用信号器来工作,所以我们必须在服务器场中的所有服务器中使用相同的机器密钥。我们可以手动进入IIS管理器并生成将包含在web.config文件中的密钥。但是因为我们使用Jenkins进行自动部署,所以我们也需要自动部署。我试图通过命令行找到一种方法,但找不到任何资源。是否有方法生成机器密钥并将其添加到web.config,以便所有过程都是自动的

您可以使用powershell通过命令行生成机器密钥。Microsoft提供此代码,可在此处找到()

其中,
$webConfigPath
是服务器上web配置文件的路径。 您需要在服务器本身上生成机器密钥,以便我们可以通过您在jenkins上的CI远程运行这些密钥。如果创建一个结合了这些函数的powershell脚本(称为
GenerateMachineKey.ps1
):

传入web.config路径,然后按如下方式运行:

Invoke-Command -ComputerName <ipAddress or computer name> -FilePath GenerateMachineKey.ps1 -ArgumentList c:\DeployedApps\YourWebsite\Web.config -credential $Credentials
现在,您可以使用这些powershell脚本通过jenkins或任何其他CI平台完全自动设置IIS机器密钥

function ApplyMachineKeyToWebConfigs($machineKey)
{
    $webConfig = (Get-Content $webConfigPath) -as [Xml]

    if($webConfig.configuration["system.web"].machineKey)
    {
        $webConfig.configuration["system.web"].ReplaceChild($webConfig.ImportNode($machineKey.machineKey, $true), $webConfig.configuration["system.web"].machineKey)
    }
    else 
    {
        $webConfig.configuration["system.web"].AppendChild($webConfig.ImportNode($machineKey.machineKey, $true))
    }

    $webConfig.save($webConfigPath)
}
[xml]$machineKey = Generate-MachineKey
ApplyMachineKeyToWebConfigs $machineKey
Invoke-Command -ComputerName <ipAddress or computer name> -FilePath GenerateMachineKey.ps1 -ArgumentList c:\DeployedApps\YourWebsite\Web.config -credential $Credentials
$Username = 'admin'
$Password = 'password'
$SecurePass = ConvertTo-SecureString -AsPlainText $Password -Force
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePass