2017-06-05 5 views
1

Jenkinsには、コミットが自動的に展開される負荷分散環境があります。私たちは働くためにSignalRを使用しているので、ファーム内のすべてのサーバに同じマシン鍵を持たなければなりません。手動でIISマネージャーに入り、web.configファイルに含まれるキーを生成することで、これを行うことができます。しかし、Jenkinsを自動展開に使用しているので、これも自動化する必要があります。私はコマンドラインでこれを行う方法を見つけようとしましたが、リソースを見つけることができませんでした。マシンキーを生成してweb.configに追加して、すべてのプロセスが自動的になるようにする方法はありますか?IISはJenkinsのコマンドラインでマシンキーを生成します

答えて

1

powershellを使用してコマンドラインからマシンキーを生成できます。マイクロソフトは、これは文字列として、マシンキーを返す

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

このコードを提供し、ここで(https://support.microsoft.com/en-ca/help/2915218/resolving-view-state-message-authentication-code-mac-errors#bookmark-appendixa)見つけることができます。これをpowershell経由でweb.configファイルに適用することもできます。次のコードは、どちらかそれを追加したり、それを$webConfigPathは、サーバー上のWeb設定ファイルへのパスです

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

に置き換えられます。 ジェンキンでCIを介してリモートで実行できるように、サーバー自体にマシンキーを生成する必要があります。あなたは(GenerateMachineKey.ps1と呼ばれる)の機能を組み合わせたPowerShellスクリプトを作成する場合:

[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 

あなたは今、完全にこれらのポウを使用して、ジェンキンス経由でIISのマシンキーまたは任意の他のCIのプラットフォームを設定し自動化することができ、次のコマンドを用いて達成することができるコマンドrshellスクリプト

関連する問題