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); }
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)); }
protected void Page_Load(object sender, EventArgs e) { HttpCookie encrypted = Request.Cookies["test"]; if (encrypted != null) { HttpCookie decrypted = HttpCookieEncryption.Decrypt(encrypted); this.txt1.Text = encrypted.Value; this.txt2.Text = decrypted.Value; } }
private string GetCookie(string ckname, string key) { string value = ""; HttpCookie cookie = HttpContext.Request.Cookies.Get(ckname); if (cookie == null) { return(value); } cookie = HttpCookieEncryption.Decrypt(cookie); value = cookie[key]; return(value); }
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); }
//public static JwtSecurityToken GenerateToken(string user, string wgs, string ds, string origin) //{ // //var now = DateTime.UtcNow; // //var tokenHandler = new JwtSecurityTokenHandler(); // ////var signingCert = getCert(); // //var tokenDescriptor = new SecurityTokenDescriptor // //{ // // Subject = new ClaimsIdentity(new[] // // { // // new Claim(ClaimTypes.Name, user), // // new Claim("wgs", wgs), // // new Claim("ds", ds), // // new Claim("origin", origin) // // }), // // Audience = "inreachweb", // // IssuedAt = now, // // Issuer = "inReachWeb", // // Expires = now.AddDays(1) // //}; // //var token = (JwtSecurityToken) tokenHandler.CreateToken(tokenDescriptor); // //return token; // return null; //} //public static string WriteToken(JwtSecurityToken token) //{ // var tokenHandler = new JwtSecurityTokenHandler(); // return tokenHandler.WriteToken(token); //} public static string GetCookieValue(string cookieName, HttpResponseBase response) { var cookie = HttpCookieEncryption.Decrypt(response.Cookies[cookieName]); return(cookie.Value); }