Article ID: 312906 - Last Review: July 11, 2005 - Revision: 3.10 How to create keys by using Visual C# .NET for use in Forms authenticationThis article was previously published under Q312906 On This PageSUMMARY This article describes how to create keys to use for
encryption, decryption, and validation of Forms authentication cookie data. You
can use the keys that you create in this article for the validationKey and decryptionKey attributes of the <machineKey> section in the <system.web> element in the Machine.config file. RequirementsThe following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
Create the projectCreate a Visual C# .NET console application:
Write the code to generate the keysThe following code reads two arguments that are passed from the command line:
Note The hexadecimal string that is created is twice the size of the value that is passed on the command line. For example, if you specify 24 bytes for a key, the resulting string is 48 bytes in length after the conversion. The valid values for decryptionKey is 8 or 24. This creates a 16 byte key for Data Encryption Standard (DES) or a 48 byte key for Triple DES, respectively. Valid values for validationKey are 20 to 64. This creates keys from 40 to 128 bytes in length. The output from the code is an entire <machineKey> element that you can copy and paste into a Machine.config file. Add the following code to a .cs file: Generate the hashesNow you can compile the application.Run the application from a command prompt by passing in two integer values that are the size of the decryption and the validation keys. For example, if you named the console application HashConfigCs.exe, type the following syntax from the command line in the Bin\debug directory of the application: hashconfigcs.exe 24 64 You can expect the application to return output that is similar to the
following output:
<machineKey validationKey="21F090935F6E49C2C797F69BBAAD8402ABD2EE0B667A8B44EA7DD4374267A75D7AD972A119482D15A4127461DB1DC347C1A63AE5F1CCFAACFF1B72A7F0A281B"
decryptionKey="261F793EB53B761503AC445E0CA28DA44AA9B3CF06263B77"
validation="SHA1"/>
Update the configuration file
TroubleshootingMake sure that the <machineKey> section has identical, explicit keys (that is, do not use the AutoGenerate option for attributes in the <machineKey> section) across the Web farm in the following scenarios:
More informationThe machineKey section should be the same across the web farm in the following cases:
REFERENCES
For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
301240
(http://support.microsoft.com/kb/301240/
)
How to implement forms-based authentication in your ASP.NET application by using C# .NET
311495
(http://support.microsoft.com/kb/311495/
)
How to implement role-based security with forms-based authentication in your ASP.NET application by using Visual C# .NET
306590
(http://support.microsoft.com/kb/306590/
)
INFO:
ASP.NET security overview
307626
(http://support.microsoft.com/kb/307626/
)
INFO:
ASP.NET configuration overview
| Article Translations
|
Back to the top
