Click here to Skip to main content
15,304,157 members
Articles / Web Development / ASP.NET
Posted 5 Sep 2006


21 bookmarked

Machine Key Generator

Rate me:
Please Sign up or sign in to vote.
2.69/5 (10 votes)
5 Sep 2006
This module is a simple windows application that generates machine keys for web farm

Sample screenshot


The <machineKey> Element configures keys to use for encryption and decryption of forms authentication cookie data and viewstate data, and for verification of out-of-process session state identification. This section can be declared at the machine, site, and application levels, but not at the subdirectory level. Using this pattern allows your application to save your authentication ticket, in away that you can travel around different websites that have the same machine key using this ticket.

Machine Key

Machine key comes in the following format:







The Code

// This line defines an object from RNGCryptoServiceProvider calss that generates a random number
// using the implementation provided by the crytographic service provider
// static RNGCryptoServiceProvider randomCryptoString = new RNGCryptoServiceProvider();

And to generate some random data using RNG (Random Number Generator)

// 64 bytes validation key is max size supported by ASP.NET
// 24 bytes decryption key is max size supported by ASP.NET
static byte[] GetRandomCryptoData(int keySize)
    byte[] randomData = new byte[keySize];
    return randomData;

Finally, we need to convert random generated data into hexadecimal:

static string ConvertKeyToHexadecimal(byte[] key)
    StringBuilder hexaString = new StringBuilder(); for (int i = 0; i < key.Length; ++i)
    hexaString.Append(String.Format("{0:X2}", key[i]));
    return hexaString.ToString();


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


About the Author

Ahmad Al-Najjar
Architect Cubic Art
Jordan Jordan
Mainly working with SharePoint, C# (Web & Desktop) and Microsofts Technologies in general ...

Comments and Discussions

QuestionHow shall I use this in Web Farm ? Pin
Prash24-Oct-06 21:14
MemberPrash24-Oct-06 21:14 
AnswerRe: How shall I use this in Web Farm ? Pin
Ahmad Al-Najjar6-Dec-06 5:35
MemberAhmad Al-Najjar6-Dec-06 5:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.