After a complete reading you can encrypt/decrypt cookies in ASP.NET 2.0 and be sure that this article is targeted to ASP.NET 2.0 only. There is a class "CookieProtectionHelper", in "System.Web.Security" that is used internally (by the Framework) to encrypt cookies and is not accessible outside the library due to its protection level. Further, we are going to use this class using Reflection and access its methods namely "Encode" and "Decode".
The following class contains the two methods "Encrypt" and "Decrypt" that actually invoke the "Encode" and "Decode" methods of the "CookieProtectionHelper" class, respectively. And provide the facility to encrypt or decrypt a cookie.
- public static class CookieSecurityProvider
- {
- private static MethodInfo _encode;
- private static MethodInfo _decode;
- // CookieProtection.All enables 'temper proffing' and 'encryption' for cookie
- private static CookieProtection _cookieProtection = CookieProtection.All;
- //Static constructor to get reference of Encode and Decode methods of Class CookieProtectionHelper
- //using Reflection.
- static CookieSecurityProvider()
- {
- Assembly systemWeb = typeof(HttpContext).Assembly;
- Type cookieProtectionHelper = systemWeb.GetType(System.Web.Security.CookieProtectionHelper");
- _encode = cookieProtectionHelper.GetMethod("Encode", BindingFlags.NonPublic | BindingFlags.Static);
- _decode = cookieProtectionHelper.GetMethod("Decode", BindingFlags.NonPublic | BindingFlags.Static);
- }
- public static HttpCookie Encrypt(HttpCookie httpCookie)
- {
- byte[] buffer = Encoding.Default.GetBytes(httpCookie.Value);
- //Referencing the Encode mehod of CookieProtectionHelper class
- httpCookie.Value = (string)_encode.Invoke(null, new object[] { _cookieProtection, buffer, buffer.Length });
- return httpCookie;
- }
- public static HttpCookie Decrypt(HttpCookie httpCookie)
- {
- //Referencing the Decode mehod of CookieProtectionHelper class
- byte[] buffer = (byte[])_decode.Invoke(null, new object[] { _cookieProtection, httpCookie.Value });
- httpCookie.Value = Encoding.Default.GetString(buffer, 0, buffer.Length);
- return httpCookie;
- }
- }


Deva RajanPosted Jan 13, 2020, 10:09 AM
Hi Manish, I am also getting the error as "Object of type 'System.Int32' cannot be converted to type 'System.Web.Security.Cryptography.Purpose'. this error comes". Can you provide the fix.
Sukhvinder SinghPosted Apr 25, 2018, 6:54 AM
Object of type 'System.Int32' cannot be converted to type 'System.Web.Security.Cryptography.Purpose'. this error comes
Ramell LamarPosted Sep 14, 2014, 3:36 PM
Can you provide some sample code on how to use the "System.Web.Security. MachineKey class " to achieve the same results as your example above?
Blocked AccountPosted Feb 15, 2013, 6:51 AM
Microsoft has changed these method in .net 4.0. So this code will through error(s). But Microsoft has introduced System.Web.Security. MachineKey class with public Encode() and Decode() methods which do the same.
Mahesh ChandPosted Feb 14, 2013, 7:14 PM
Can you also use the same code/class in ASP.NET 4.0 or 4.5? If yes, then I would probably change the title.