Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you set the MachineKey programmatically?

In ASP.NET can you set the machineKey settings programmatically?

The web app that we use stores sensitive info encrypted in a database, so if we could put the decryptionKey there it would be handy.

like image 224
Mr. Flibble Avatar asked Sep 20 '12 14:09

Mr. Flibble


People also ask

How do I set up MachineKey?

If you have access to the IIS management console for the server where Orchard is installed, it is the easiest way to set-up a machine key. Uncheck "Automatically generate at runtime" for both the validation key and the decryption key. Click "Generate Keys" under "Actions" on the right side of the panel. Click "Apply".

What is MachineKey validationKey?

"validationKey specifies a manually assigned validation key. This value must be manually set to ensure consistent configuration across a network of Web servers (a Web farm). The key must be a minimum of 40 characters (20 bytes) and a maximum of 128 characters (64 bytes) long.

What is MachineKey?

Defines the configuration settings that control the key generation and algorithms that are used in encryption, decryption, and message authentication code (MAC) operations in Windows Forms authentication, view-state validation, and session-state application isolation.


2 Answers

No; the machineKey element must be set via config. However, web.config can itself be encrypted, which helps minimize risk of cryptographic key disclosure if an attacker ever gets access to the config file. (This same process can be used to protect SQL connection strings and pretty much any other sensitive config element you wish.) See http://msdn.microsoft.com/en-us/library/dtkwfdky(v=VS.100).aspx for a walkthrough on enabling this.

like image 84
Levi Avatar answered Nov 13 '22 12:11

Levi


Yes, you can. I got success using this code in ConsoleApplication:

private static void ChangeWebConfig(string validationKey, string decryptionKey, string webConfigPath)
{
    ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
    configFileMap.ExeConfigFilename = webConfigPath;
    System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
    MachineKeySection section = (MachineKeySection)config.GetSection("system.web/machineKey");
    section.ValidationKey = validationKey;
    section.DecryptionKey = decryptionKey;
    config.Save();
}
like image 45
Julian Corrêa Avatar answered Nov 13 '22 10:11

Julian Corrêa