Ejemplo n.º 1
0
        /// <summary>
        /// Decrypts the cookie.
        /// </summary>
        /// <param name="cookie">The cookie to decrypt.</param>
        /// <returns>A new HttpCookie instance with the Value decrypted.</returns>
        public static HttpCookie Decrypt(HttpCookie cookie)
        {
            if (cookie == null)
            {
                throw new ArgumentNullException("cookie");
            }

            //nothing to do!
            if (cookie.Value.Length == 0)
            {
                return(cookie);
            }

            try
            {
#if NET10 || NET11 || NET20 || NET30 || NET35
                byte[] encrypted = MachineKeyWrapper.HexStringToByteArray(cookie.Value);
                if (encrypted == null)
                {
                    return(null);                                       // i wonder if this is the most intuitive situation here... the above method will return null if it cant "DeHex" the string...
                }
                byte[] decrypted = MachineKeyWrapper.EncryptOrDecryptData(false, encrypted, null, 0, encrypted.Length);

                //i wonder if I should guarantee getting a cookie at this point [no exceptions, etc]
                HttpCookie decryptedCookie = CloneCookie(cookie);

                decryptedCookie.Value = System.Text.Encoding.Unicode.GetString(decrypted);
                return(decryptedCookie);
#else
                HttpCookie decryptedCookie = CloneCookie(cookie);
                decryptedCookie.Value = MachineKeyWrapper.Unprotect(cookie.Value);
                return(decryptedCookie);
#endif
            }
            catch (Exception ex)
            {
                //repackage the exception
                throw new HttpException("Unable to Decrypt the cookie.", ex);
            }
        }