IIS通过命令行生成机器密钥



我们与詹金斯(Jenkins(有一个负载平衡的环境,在该环境中自动部署了提交。由于我们正在使用SignalR来工作,因此我们必须在农场的所有服务器中具有相同的机器密钥。我们可以通过手动进入IIS管理器并生成将包含在Web.config文件中的密钥来做到这一点。但是,由于我们正在使用Jenkins进行自动部署,因此我们也需要自动。我试图找到一种通过命令行进行此操作的方法,但找不到任何资源。有没有一种方法可以生成机器密钥并将其添加到Web.config中,以便所有过程都是自动的?

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

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:DeployedAppsYourWebsiteWeb.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机器密钥

最新更新