Ejemplo n.º 1
0
        /// <summary>
        /// Returns an encrypted cookie without updating the Response.
        /// </summary>
        /// <param name="source">The cookie to encrypt</param>
        /// <returns>An encrypted instance, cloned from the source.</returns>
        public static HttpCookie Encrypt(HttpCookie source)
        {
            try
            {
#if NET10 || NET11 || NET20 || NET30 || NET35
                byte[] data    = System.Text.Encoding.Unicode.GetBytes(source.Value);
                byte[] encData = MachineKeyWrapper.EncryptOrDecryptData(true, data, null, 0, data.Length);

                HttpCookie encrypted = CloneCookie(source);
                encrypted.Value = MachineKeyWrapper.ByteArrayToHexString(encData, encData.Length);
                return(encrypted);
#else
                HttpCookie encrypted = CloneCookie(source);
                encrypted.Value = MachineKeyWrapper.Protect(source.Value);
                return(encrypted);
#endif
            }
            catch (Exception ex)
            {
                //repackage
                throw new HttpException("Unable to encrypt the cookie.", ex);
            }
        }