How to Generate machineKey for your web.config in .NET

No.of Views3414
Bookmarked0 times
By  Mostafa Elzoghbi   On  03 Oct 2010 09:10:04
Tag : ASP.NET , Security
We were implementing Single Sign-On for our enterprise ASP.NET applications, I have been asked: How can we generate our own machine keys in our web.config
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at



We were implementing Single Sign-On for our enterprise ASP.NET applications, I have been asked: How can we generate our own machine keys in our web.config ? Here is the console application you need to run and to get a random key based on the length you need and then add it to your web.config.

C# Code

static void Main(string[] keyLength)

int len = 128;
if (keyLength.Length > 0)
len = int.Parse(keyLength[0]);
byte[] buff = new byte[len / 2];
RNGCryptoServiceProvider rng = newRNGCryptoServiceProvider();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < buff.Length; i++)
sb.Append(string.Format("{0:X2}", buff[i]));


 If you didn't pass anything to this method will generate 128 character key (64 byte), If you want to get 32 byte key, you need to pass 64 as key length input.

If you are trying different cryptography algorithms provided by System.Cryptography namespace in .NET Framework so here is the reference you need to read for what is the required length for each algorithm ? either if you are using: DES,3DES,AES,SHA1...etc.

Hope this helps.

Sign Up to vote for this article
About Author
Mostafa Elzoghbi
Member Type-Junior
Location-United States
Joined date-07 Jun 2010
Home Page-
Blog Page-
Other popularSectionarticles
    Auto Growing TextBox or TextArea in ASP.NET
    Published Date : 08/May/2010
    In this code snippet, you will learn how to bind DropdownList within the ListView in ASP.NET. The ListView is powerful control and fully customizable using templates.
    Published Date : 10/Oct/2010
    In this codesnippet, i will show How to Delete Row in GridView using JQuery in ASP.NET.
    Published Date : 20/Jul/2011
    In this snippet, I will show how to format a cell and apply style in gridview using JQuery. Sometimes we may need to apply the format for a particular cell based on the cell value; it can be done in within DataRowBound event in
    Published Date : 05/Jan/2011
    In this snippet I will explain how to add controls dynamically in and register events for the controls and make it work events perfectly. Last week I have read the forums many readers asking about add controls dynamically in giving problems and also it not working properly with events
    Published Date : 03/Jan/2011
There is no comments for this articles.
Leave a Reply
Display Name:
(not display in page for the security purphase)
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top