I find myself wanting to get the ASP.NET machine key for the current application. This is, of course, easy if a machine key is specified in the configuration file, but if it's set to auto generate then there doesn't seem to be a public method anywhere to get it.
Basically I want at it so I can write an encrypted/MACed cookie for myself, just like the ASP.NET Forms Authentication provider does.
Does anyone have any pointers or ideas?
I had the same issue and needed to get the machinekey from a running web application (not using .NET 4.5 crypto features) that I could not make a code change to, so I created a simple .aspx file that extracts the key and dumps it to a file and then placed it in the application root and accessed it using a browser (without needing to touch the running application)
If the ASP.NET Forms Authentication provider can access it then have you tried looking at the provider source code? (I think this is the correct location, ScottGu's original blog post on the subject has had broken links since they updated MSDN)
If you're using .NET 4, there's the MachineKey class. It doesn't give you raw access to the actual key, but it does provide methods for Encoding and Decoding the data using the same algorithms as the FormsAuthentication class, along with options for adding validation w/ an HMAC.
I came up with this as a combination of the answers above for post-4.5 .NET. Drop the code below in a file named mk.aspx, then browse to it to get the key. Be sure to delete it immediately after, because this is evil.
Do you actually NEED the key? Or just to encrypt and decrypt the data?
System.Web.Security.FormsAuthentication (.NET 2.0) has public Encrypt/Decrypt methods. These use System.Web.Configuration.MachineKeySection EncryptOrDecryptData, ByteArrayToHexString and HexStringToByteArray to encrypt and decrypt the data.
EncryptOrDecryptData handles loading / configuring the key data from config files/AutoGenerate as required.
Encrypt And Decrypt should be available via the source code downloads or reflector and readily converted to your purpose.
Mr. Curious was curious about getting the machine key as well. The properties on the
MachineKeySection
are no good, as they get zeroed-out after initialization, which happens before you can read them with reflection.After a bit of digging in the current 4.5 framework, turns out that the auto generated keys are stored in
HttpApplication.s_autogenKeys
byte array. The validation key is the first 64 bytes, followed by 24 bytes of the decryption key.If you are not opting in into the new crypto stuff in 4.5 framework, that is, you didn't set
<httpRuntime targetFramework="4.5">
in yourweb.config
(which is the case if you have an app you created with a previous version of the framework), then you get to the keys like this:The default for both keys is
AutoGenerate,IsolateApps
; theIsolateApps
bit requires that you copy the first four bytes of the application path hash to the beginning of the key.If you opted in into the cryptographic improvements in fx4.5, then you'll have to dig around the MachineKeyMasterKeyProvider to get the valid keys.
Getting the Keys without the HttpApplication
The
HttpApplication
gets its keys by calling into a native method inwebengine4.dll
fromSetAutogenKeys()
. We can call into the DLL ourselves as well. All we need to know is our application path.Let's say that we want to get the auto generated keys for the root application, "
/
".Using LinqPad:
Getting the keys from MachineKeyMasterKeyProvider
The keys for the new fx4.5 stuff are accessible by instantiating the
MachineKeyMasterKeyProvider
with the internal constructor, and then passing inautogenKeys
byte array obtained as in the code above. The provider has methodsGetEncryptionKey
andGetValidationKey
to get to actual keys.