IIS通过Jenkins

时间:2017-06-05 12:17:19

标签: c# asp.net iis jenkins

我们在Jenkins的负载均衡环境中自动部署了提交。由于我们使用SignalR才能工作,因此我们必须在服务器场中的所有服务器中拥有相同的机器密钥。我们可以通过手动进入IIS管理器并生成将包含在web.config文件中的密钥来实现。但由于我们使用Jenkins进行自动部署,因此我们也需要自动部署。我试图通过命令行找到一种方法,但找不到任何资源。有没有办法生成机器密钥并将其添加到web.config所以所有过程将是自动的?

1 个答案:

答案 0 :(得分:2)

您可以使用powershell通过命令行生成机器密钥。 Microsoft提供此代码,可在此处找到(https://support.microsoft.com/en-ca/help/2915218/resolving-view-state-message-authentication-code-mac-errors#bookmark-appendixa

function Generate-MachineKey {
  [CmdletBinding()]
  param (
    [ValidateSet("AES", "DES", "3DES")]
    [string]$decryptionAlgorithm = 'AES',
    [ValidateSet("MD5", "SHA1", "HMACSHA256", "HMACSHA384", "HMACSHA512")]
    [string]$validationAlgorithm = 'HMACSHA256'
  )
  process {
    function BinaryToHex {
        [CmdLetBinding()]
        param($bytes)
        process {
            $builder = new-object System.Text.StringBuilder
            foreach ($b in $bytes) {
              $builder = $builder.AppendFormat([System.Globalization.CultureInfo]::InvariantCulture, "{0:X2}", $b)
            }
            $builder
        }
    }
    switch ($decryptionAlgorithm) {
      "AES" { $decryptionObject = new-object System.Security.Cryptography.AesCryptoServiceProvider }
      "DES" { $decryptionObject = new-object System.Security.Cryptography.DESCryptoServiceProvider }
      "3DES" { $decryptionObject = new-object System.Security.Cryptography.TripleDESCryptoServiceProvider }
    }
    $decryptionObject.GenerateKey()
    $decryptionKey = BinaryToHex($decryptionObject.Key)
    $decryptionObject.Dispose()
    switch ($validationAlgorithm) {
      "MD5" { $validationObject = new-object System.Security.Cryptography.HMACMD5 }
      "SHA1" { $validationObject = new-object System.Security.Cryptography.HMACSHA1 }
      "HMACSHA256" { $validationObject = new-object System.Security.Cryptography.HMACSHA256 }
      "HMACSHA385" { $validationObject = new-object System.Security.Cryptography.HMACSHA384 }
      "HMACSHA512" { $validationObject = new-object System.Security.Cryptography.HMACSHA512 }
    }
    $validationKey = BinaryToHex($validationObject.Key)
    $validationObject.Dispose()
    [string]::Format([System.Globalization.CultureInfo]::InvariantCulture,
      "<machineKey decryption=`"{0}`" decryptionKey=`"{1}`" validation=`"{2}`" validationKey=`"{3}`" />",
      $decryptionAlgorithm.ToUpperInvariant(), $decryptionKey,
      $validationAlgorithm.ToUpperInvariant(), $validationKey)
  }
}

这将机器密钥作为字符串返回。然后,您也可以通过powershell将其应用于web.config文件。以下代码将添加或替换它

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)
}

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

[xml]$machineKey = Generate-MachineKey
ApplyMachineKeyToWebConfigs $machineKey

传入web.config路径,然后运行它:

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

现在,您可以使用这些powershell脚本通过jenkins或任何其他CI平台完全自动设置IIS机器密钥