简体   繁体   中英

How do I generate IIS machineKey from inside code?

I want random values for this web.config section:

<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. So I need some other way.

I could write some C# code for that but what should it do exactly? 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? Will that do?

Do I just use RNGCryptoServiceProvider class and generate an appropriately long byte array and then format that as hex?

Pretty much. That's what the .NET Framework is doing when you set it to AutoGenerate .

You can use RNGCryptoServiceProvider to generate an hex value with an appropriate length. HMACSHA512 uses a 512-bit value, which is 64-bytes, or 128 hex characters. 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("-", "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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