Exemplo n.º 1
0
        protected void btnSet_Click(object sender, EventArgs e)
        {
            HttpCookie cookie = new HttpCookie("test");

            cookie.Value = this.txtCookieValue.Text;

            cookie = HttpCookieEncryption.Encrypt(cookie);
            Response.Cookies.Set(cookie);
        }
Exemplo n.º 2
0
        public static void RemoveCookie(string cookieName, HttpResponseBase response)
        {
            if (!CookieExists(cookieName, response))
            {
                return;
            }
            var cookie = HttpCookieEncryption.Decrypt(response.Cookies[cookieName]);

            cookie.Expires = DateTime.Now.AddDays(-1);
            response.Cookies.Set(HttpCookieEncryption.Encrypt(cookie));
        }
        public void Encrypt_Cookie()
        {
            string startingValue = "asdf";

            HttpCookie cookie = new HttpCookie("test");

            cookie.Value = startingValue;

            cookie = HttpCookieEncryption.Encrypt(cookie);
            string actualValue = cookie.Value;

            Assert.AreNotEqual(startingValue, actualValue);
        }
        public void Encrypt_Decrypt_roundtrip()
        {
            string startingValue = "asdf";

            HttpCookie cookie = new HttpCookie("test");

            cookie.Value = startingValue;

            HttpCookie encrypted = HttpCookieEncryption.Encrypt(cookie);

            HttpCookie decrypted = HttpCookieEncryption.Decrypt(encrypted);

            Assert.AreEqual(decrypted.Value, cookie.Value);
        }
        private void AddCookie(string ckname, string[] key, string[] value)
        {
            HttpCookie cookie = HttpContext.Request.Cookies.Get(ckname);

            if (cookie == null)
            {
                cookie = new HttpCookie(ckname);
                int i = 0;
                foreach (var k in key)
                {
                    cookie[k] = value[i];
                    i++;
                }
            }
            else
            {
                cookie = HttpCookieEncryption.Decrypt(cookie);
                int i = 0;
                foreach (var k in key)
                {
                    if (cookie[k] == null)
                    {
                        cookie.Values.Add(k, value[i]);
                    }
                    else
                    {
                        cookie[k] = value[i];
                    }
                    i++;
                }
            }

            HttpCookie cookie_enc = HttpCookieEncryption.Encrypt(cookie);

            this.ControllerContext.HttpContext.Response.Cookies.Add(cookie_enc);
        }