using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace PAB.Web { public static class CookieUtil { public static string GetCookieValue (string cookieName) { string cookieVal = String.Empty; cookieVal = HttpContext.Current.Request.Cookies[cookieName].Value; return cookieVal; } public static void CreateCookie (string cookieName, string value, int? expirationDays) { HttpCookie Cookie = new HttpCookie(cookieName, value); if ( expirationDays.HasValue ) Cookie.Expires = DateTime.Now.AddDays(expirationDays.Value ); HttpContext.Current.Response.Cookies.Add(Cookie); } public static void DeleteCookie (string cookieName) { HttpCookie Cookie = HttpContext.Current.Request.Cookies[cookieName]; if (Cookie != null) { Cookie.Expires = DateTime.Now.AddDays(-2); HttpContext.Current.Response.Cookies.Add(Cookie); } } public static bool CookieExists(string cookieName) { bool exists = false; HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName]; if (cookie != null) exists = true; return exists; } public static DictionaryGetAllCookies() { Dictionary cookies = new Dictionary (); foreach (string key in HttpContext.Current.Request.Cookies.AllKeys) { cookies.Add(key, HttpContext.Current.Request.Cookies[key].Value); } return cookies; } public static void DeleteAllCookies () { var x = HttpContext.Current.Request.Cookies; foreach (HttpCookie cook in x) { DeleteCookie(cook.Name); } } } }
Sunday, April 10, 2011
ASP.NET Cookie helper
http://www.eggheadcafe.com/tutorials/aspnet/e7108c33-1cc6-48ae-9f65-cc391c8b66a7/aspnet-generic-cookie-utility-class.aspx
Subscribe to:
Post Comments (Atom)
Very helpful!
ReplyDeleteThanks a bunch!
ReplyDeleteThanks for your valuable article. Great post.
ReplyDeleteSoftware Testing Training in Chennai | Selenium Training in Chennai
Great blog! Thanks for giving such valuable information, this is unique one. Really admired
ReplyDeleteDot Net Training in Chennai
Nice! you are sharing such helpful and easy to understandable blog. i have no words for say i just say thanks because it is helpful for me.
ReplyDeleteDot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery