public static void AddCookie(this List<Cookie> cookieList, string setCookieFormat) { var bits = setCookieFormat.Split(';'); string key, value; GetKeyValue(bits[0], '=', out key, out value); var c = new Cookie(key, value); for(var i = 1; i < bits.Length; i++) { GetKeyValue(bits[1], '=', out key, out value); switch(key) { case "expires": c.Expires = DateTime.Parse(value); break; case "path": c.Path = value; break; case "domain": c.Domain = value; break; case "secure": c.Secure = value == "true"; break; } } cookieList.AddCookie(c); }
/// <summary> /// Safely adds a cookie to client browser. The existing cookie (by name) will be overwritten /// </summary> /// <param name="context"></param> /// <param name="cookieName"></param> /// <param name="cookieValue"></param> /// <param name="expiredInDay"></param> public static void AddCookie(this HttpContextBase context, string cookieName, string cookieValue, int expiredInDay = 90) { HttpCookie cookie = new HttpCookie(cookieName); cookie.Value = cookieValue; cookie.Expires = DateTime.Now.AddDays(expiredInDay); context.AddCookie(cookie); }
private static void AddAuthenticationCookie(this Response response, IAuthenticationTokenService authenticationTokenService, ChatUser user) { string userToken = authenticationTokenService.GetAuthenticationToken(user); var cookie = new NancyCookie(Constants.UserTokenCookie, userToken, httpOnly: true) { Expires = DateTime.Now + TimeSpan.FromDays(30) }; response.AddCookie(cookie); }
public static void LoadCookiesFromNSFile(this List<Cookie> cookieList, string filename) { var cookieLines = File.ReadAllLines(filename); foreach(string c in cookieLines) { if(c.StartsWith(";") || c.StartsWith("#")) { continue; } try { var bits = c.Split('\t'); var cookie = new Cookie(bits[5], bits[6]) { Domain = bits[0], Path = bits[2], Secure = bits[3].IsTrue(), Expires = (new DateTime(1970, 1, 1, 0, 0, 0)).AddSeconds(bits[4].ToInt32()) }; cookieList.AddCookie(cookie); } // ReSharper disable EmptyGeneralCatchClause catch(Exception) { } // ReSharper restore EmptyGeneralCatchClause } }
public static void AddCookies(this List<Cookie> cookieList, CookieCollection cookies) { foreach(Cookie c in cookies) { cookieList.AddCookie(c.Clone()); } }
/// <summary> /// Delete a cookie specified by <c>cookieName</c> /// </summary> /// <param name="context"></param> /// <param name="cookieName"></param> public static void DeleteCookie(this HttpContextBase context, string cookieName) { if (context.Request.Cookies[cookieName] != null) { HttpCookie cookie = new HttpCookie(cookieName) {Expires = DateTime.Now.AddDays(-1d)}; context.AddCookie(cookie); } }