Bu makalemde asp.net projesi içerisindeki cookieleri nasıl encyript ve decrypt yapıcağımızı göstereceğim. Bunun için ise “MachineKey.Protect” and “MachineKey.Unrotect” methodlarını kullanacağız.
Encrypt Cookies:
//using System.Text; //using System.Web.Security; var cookieText = Encoding.UTF8.GetBytes("Text for Cookie"); var encryptedValue = Convert.ToBase64String(MachineKey.Protect(cookieText, "ProtectCookie")); //--- Create cookie object and pass name of the cookie and value to be stored. HttpCookie cookieObject = new HttpCookie("NameOfCookie", encryptedValue); //---- Set expiry time of cookie. cookieObject.Expires.AddDays(5); //---- Add cookie to cookie collection. Response.Cookies.Add(cookieObject);
Decrypt Cookies:
var bytes = Convert.FromBase64String(Request.Cookies["NameOfCookie"].Value); var output = MachineKey.Unprotect(bytes, "ProtectCookie"); string result = Encoding.UTF8.GetString(output);
Reklamlar