Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C#

Single Sign-on in ASP.NET and Other Platforms

Rate me:
Please Sign up or sign in to vote.
4.90/5 (42 votes)
7 Jul 2008CPOL8 min read 320.3K   7.8K   204  
Discuss the concept of single sign-on using Forms authentication within ASP.NET as well as other platforms.
using System;
using System.Security;
using System.Security.Cryptography;
using System.Text;

public partial class GenerateMachineKey : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateKey();
    }
    public void CreateKey()
    {
        //The hexadecimal string that is created is twice the size of the value that is passed in. 
        //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.
        
        int DecryptionArg = 24;
        int ValidationArg = 32;
        lblValidationKey.Text = "validationKey=" + CreateKey(ValidationArg) + "<br>";
        lblDecryptionKey.Text = "decryptionKey=" + CreateKey(DecryptionArg) + "<br>";
        lblValidation.Text="validation=SHA1";
    }

    private String CreateKey(int numBytes)
    {
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        byte[] buff = new byte[numBytes];

        rng.GetBytes(buff);
        return BytesToHexString(buff);
    }

    private String BytesToHexString(byte[] bytes)
    {
        StringBuilder hexString = new StringBuilder(64);

        for (int counter = 0; counter < bytes.Length; counter++)
        {
            hexString.Append(String.Format("{0:X2}", bytes[counter]));
        }
        return hexString.ToString();
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Web & Database Developer. Design and implement web and database applications utilizing Microsoft and other development tools.

Comments and Discussions