我们有一个负载均衡的环境,Jenkins会自动部署提交。由于我们使用SignalR来工作,我们必须在服务器场中的所有服务器中拥有相同的机器密钥。我们可以通过手动进入IIS管理器并生成将包含在web.config文件中的密钥来完成。但由于我们正在使用Jenkins进行自动部署,我们需要这也是自动的。我试图通过命令行找到一种方法,但找不到任何资源。有没有一种方法可以生成机器密钥并将其添加到web.config中,以便所有进程都将自动进行?IIS通过命令行为Jenkins生成机器密钥
1
A
回答
1
您可以使用powershell通过命令行生成机器密钥。微软提供这些代码,并可以在这里(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
):在web.config路径传递
[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
现在可以完全自动通过詹金斯或使用这些鲍威任何其他CI平台设置你的IIS机器密钥rshell脚本
相关问题
- 1. 通过Jenkins运行命令?
- 2. 使用KeyTool命令生成密钥库
- 3. 通过命令行将密钥库密码传递给Cordova Android
- 4. 通过命令行只(生成文件)
- 5. 通过命令行只(生成文件)
- 6. 随机密钥生成
- 7. 密钥工具证书生成appeats在命令行中挂
- 8. 使用电源生成机器密钥壳牌 - 无法识别的命令
- 9. 通过命令指定电子邮件地址在Windows上生成SSH密钥
- 10. 对称加密密钥通过行为
- 11. 如何通过命令行参数导入RSA密钥?
- 12. 通过EMAIL在Jenkins触发器生成
- 13. AS3 Php密钥生成器
- 14. C RSA密钥生成器
- 15. Rails无法通过Windows中的命令行查找生成器
- 16. 使用随机生成的密钥进行加密和解密?
- 17. 为ConfigurationProperty密钥生成ID?
- 18. 密钥生成
- 19. 生成密钥
- 20. opencover报告生成器通过命令提示符运行,但不通过ccnet.config
- 21. 生成命令行
- 22. 如何借助密钥生成令牌?
- 23. 通过rsh命令密码?
- 24. 如何从Windows命令提示符生成公钥密码
- 25. 如何从node.js生成ssh命令时传递密钥文件?
- 26. KeyPairGenerator没有生成随机密钥
- 27. iOS生成RSA非随机密钥对?
- 28. Ruby OpenSSL AES生成随机密钥
- 29. 通过命令行从运行unittest.TestCase生成单个测试
- 30. 通过密钥