Export (0) Print
Expand All

MachineKey Class

Provides a way to encrypt or hash data (or both) by using the same algorithms and key values that are used for ASP.NET forms authentication and view state.

System.Object
  System.Web.Security.MachineKey

Namespace:  System.Web.Security
Assembly:  System.Web (in System.Web.dll)

public static class MachineKey

The MachineKey type exposes the following members.

  NameDescription
Public methodStatic memberDecodeDecodes and/or validates data that has been encrypted or provided with a hash-based message authentication code (HMAC).
Public methodStatic memberEncodeEncrypts data and/or appends a hash-based message authentication code (HMAC).
Top

ExceptionCondition
ArgumentNullException

The data to encrypt, hash, decrypt, or validate does not exist

The MachineKey class provides methods that expose the hashing and encryption logic that ASP.NET provides. For information about which encryption and hashing algorithms ASP.NET uses, and the key values that it uses with them, see machineKey Element (ASP.NET Settings Schema).

The following example shows how to encrypt, hash, decrypt, and validate a string. The code accepts a string that you enter, encrypts the data and adds a hash value to it using one call to the Encode(Byte[], MachineKeyProtection) method, and then displays the encrypted data. The code then decrypts and validates the encrypted data in one call to the Decode(String, MachineKeyProtection) method, and displays the decrypted data in another label. The Encode(Byte[], MachineKeyProtection) and Decode(String, MachineKeyProtection) methods accept a parameter that specifies whether they should encrypt or decrypt, hash or validate, or do both. In this example they do both.


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(DataToEncrypt.Text))
        {
            Byte[] byteArray = 
                System.Text.Encoding.Unicode.GetBytes
                    (DataToEncrypt.Text);
            EncryptedData.Text = 
                System.Web.Security.MachineKey.Encode
                    (byteArray, MachineKeyProtection.All);

            DecryptedData.Text = 
                System.Text.Encoding.Unicode.GetString
                    (System.Web.Security.MachineKey.Decode
                        (EncryptedData.Text, MachineKeyProtection.All));
        }            
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>MachineKey Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>MachineKey Example</h1>
        Enter a string to be encrypted: 
            <asp:TextBox ID="DataToEncrypt" runat="server">
            </asp:TextBox>
            <br />
        The encrypted value is: 
            <asp:Label ID="EncryptedData" runat="server" Text="">
            </asp:Label>
            <br />
        The decrypted value is: 
            <asp:Label ID="DecryptedData" runat="server" Text="">
            </asp:Label>
            <br />
        <asp:Button ID="EncryptDecrypt" runat="server" 
            Text="Encrypt and Decrypt" />
    </div>
    </form>
</body>
</html>


.NET Framework

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft