示例#1
0
        /// <summary>
        /// Decodes a cookie from definable protection level.
        /// </summary>
        /// <param name="cookie">
        /// The encoded cookie to decode of type <see cref="System.Web.HttpCookie"/> .
        /// </param>
        /// <param name="cookieProtection">
        /// An enumeration of type <see cref="System.Web.Security.CookieProtection"/> defining the level of protection to use when decoding the cookie.
        /// </param>
        /// <returns>
        /// An object of type <see cref="System.Web.HttpCookie"/> which is the decoded cookie.
        /// </returns>
        /// <exception cref="CookieSecureException">
        /// Thrown if
        ///     <paramref name="cookie"/>
        ///     is invalid or tampered.
        /// </exception>
        /// <example>
        /// The following C# example shows how to decode a cookie that has been encoded using "Encryption" only.
        ///     <code>
        /// HttpCookie standardCookie = Request.Cookies["MyCookieName"];
        ///     if(standardCookie!=null)
        ///     {
        ///     HttpCookie decodedCookie = Rolosoft.WebControls.CookieSecure.Decode(standardCookie,System.Web.Security.CookieProtection.Encryption);
        ///     //...Go on to process decoded cookie
        ///     }
        /// </code>
        /// </example>
        public static HttpCookie Decode(HttpCookie cookie, CookieProtection cookieProtection)
        {
            if (cookie != null)
            {
                var decodedCookie = CloneCookie(cookie);
                decodedCookie.Value = MachineKeyCryptography.Decode(cookie.Value, cookieProtection);
                return(decodedCookie);
            }

            return(null);
        }
示例#2
0
        /// <summary>
        /// Encodes a cookie with definable protection levels.
        /// </summary>
        /// <param name="cookie">
        /// An object of type <see cref="System.Web.HttpCookie"/> to encode.
        /// </param>
        /// <param name="cookieProtection">
        /// An enumeration of type <see cref="System.Web.Security.CookieProtection"/> defining the level of protection required.
        /// </param>
        /// <returns>
        /// An object of type <see cref="System.Web.HttpCookie"/> encoded and tamper proof.
        /// </returns>
        /// <exception cref="CookieSecureException">
        /// Thrown if
        ///     <paramref name="cookie"/>
        ///     is invalid or tamper is detected.
        /// </exception>
        /// <example>
        /// The following C# example demonstrates how to Encode a cookie.
        ///     <code>
        /// //Create a value to store
        ///     string myValueToStoreInCookie = "My details";
        ///
        ///     //Create a "standard" (unencrypted) HttpCookie
        ///     HttpCookie myCookie = new HttpCookie("MyCookieName",myValueToStoreInCookie);
        ///
        ///     //encrypt the "standard" cookie
        ///     HttpCookie myEncodedCookie = CookieSecure.Encode(myCookie, System.Web.Security.CookieProtection.Encryption);
        ///
        ///     //Add the encoded cookie to the response stream.
        ///     Page.Response.AppendCookie(myEncodedCookie);
        /// </code>
        /// </example>
        public static HttpCookie Encode(HttpCookie cookie, CookieProtection cookieProtection)
        {
            try
            {
                if (cookie != null)
                {
                    var encodedCookie = CloneCookie(cookie);
                    encodedCookie.Value = MachineKeyCryptography.Encode(cookie.Value, cookieProtection);
                    return(encodedCookie);
                }

                return(null);
            }
            catch (Exception ex)
            {
                throw new CookieSecureException(Resources.CookieError2, ex.InnerException);
            }
        }