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

No.of Views3238
Bookmarked0 times
Downloads 
Votes0
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 asp.net 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 info@codegain.com

 

Introduction

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 asp.net 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();
rng.GetBytes(buff);
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < buff.Length; i++)
sb.Append(string.Format("{0:X2}", buff[i]));
Console.WriteLine(sb);

}

 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.

http://msdn.microsoft.com/en-us/library/w8h3skw9.aspx

Hope this helps.
 

 
Sign Up to vote for this article
 
About Author
 
Mostafa Elzoghbi
Occupation-
Company-
Member Type-Junior
Location-United States
Joined date-07 Jun 2010
Home Page-http://moustafa-arafa.blogspot.com
Blog Page-http://moustafa-arafa.blogspot.com
MCSD,MCT,MCTS,C# MVP
 
 
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 asp.net.
    Published Date : 05/Jan/2011
    In this snippet I will explain how to add controls dynamically in asp.net 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 asp.net giving problems and also it not working properly with events
    Published Date : 03/Jan/2011
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
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