Click here to Skip to main content
Licence 
First Posted 5 Sep 2006
Views 24,608
Bookmarked 17 times

Machine Key Generator

By | 5 Sep 2006 | Article
This module is a simple windows application that generates machine keys for web farm

Download Machine Key Generator

Sample screenshot

Introduction

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:

<system.web>
<machineKey 
validationKey='9E7338046712DC5407684161A2B7F67AE74FF304B005B4B32587BB6BAF89C1FB99E886D5637A771912362466ED9DD4FA9C9BCEBB682918E2536542DD85427FD9'   
decryptionKey='EF975B401B6327D5D773435F2F0172B81CD19C7EB515FA58'   
validation='SHA1'/>
system.web>

 

The Code

 

<P>//
// 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();</P>

 

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

 

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

 

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

 

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

License

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

Member

Follow on Twitter Follow on Twitter
Mainly working with SharePoint, C# (Web & Desktop) and Microsofts Technologies in general ...

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow shall I use this in Web Farm ? Pinmemberprutuja21:14 24 Oct '06  
AnswerRe: How shall I use this in Web Farm ? PinmemberAhmad AlNajjar5:35 6 Dec '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 5 Sep 2006
Article Copyright 2006 by Ahmad Al-Najjar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid