简体   繁体   English

如何从内部代码生成IIS machineKey?

[英]How do I generate IIS machineKey from inside code?

I want random values for this web.config section: 我想要此web.config部分的随机值:

<machineKey validation="HMACSHA512" decryption="AES"
     validationKey="I NEED VALUE FOR THIS" 
     decryptionKey="SOME KEY" />

I see that IIS Manager can generate values but in the version I have it has no option for "HMACSHA512" in the dropdown. 我看到IIS管理器可以生成值,但是在我拥有的版本中,下拉菜单中没有用于“ HMACSHA512”的选项。 So I need some other way. 所以我需要其他方法。

I could write some C# code for that but what should it do exactly? 我可以为此编写一些C#代码,但是它应该做什么呢? Should it just generate a long enough random number and format that? 它应该生成足够长的随机数和格式吗?

Do I just use RNGCryptoServiceProvider class and generate an appropriately long byte array and then format that as hex? 我是否只使用RNGCryptoServiceProvider类并生成适当的长字节数组,然后将其格式化为十六进制? Will that do? 可以吗?

Do I just use RNGCryptoServiceProvider class and generate an appropriately long byte array and then format that as hex? 我是否只使用RNGCryptoServiceProvider类并生成适当的长字节数组,然后将其格式化为十六进制?

Pretty much. 差不多了 That's what the .NET Framework is doing when you set it to AutoGenerate . 这就是.NET Framework在将其设置为AutoGenerate所做的事情

You can use RNGCryptoServiceProvider to generate an hex value with an appropriate length. 您可以使用RNGCryptoServiceProvider生成具有适当长度的十六进制值。 HMACSHA512 uses a 512-bit value, which is 64-bytes, or 128 hex characters. HMACSHA512使用512位值,即64字节或128个十六进制字符。 So we can do something like this: 所以我们可以做这样的事情:

var rng = System.Security.Cryptography.RandomNumberGenerator.Create();
var buffer = new byte[64];
rng.GetBytes(buffer);
var hmacKey = BitConverter.ToString(buffer).Replace("-", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM