public static void AddCookie(HttpResponseBase Response, string name, string value, int day, int hours, int minutes, int seconds) { HttpCookie cookie = new HttpCookie(name); TimeSpan ts = new TimeSpan(day, hours, minutes, seconds); cookie.Expires = DateTime.Now.Add(ts); cookie.Value = value; Response.AppendCookie(cookie); }
private void PrepareResponse(HttpResponseBase response, string downloadTokenValue, string zipFilename) { response.ContentType = "application/zip"; if (string.IsNullOrEmpty(downloadTokenValue) == false) { //downloadTokenValue will have been provided in the form submit via the hidden input field response.AppendCookie(new HttpCookie("fileDownloadToken", downloadTokenValue)); } response.AddHeader("content-disposition", string.Format("attachment; filename={0}.zip", zipFilename)); }
/// <summary> /// Authenticates a user via the MembershipProvider and creates the associated forms authentication ticket. /// </summary> /// <param name="logon">Logon</param> /// <param name="response">HttpResponseBase</param> /// <returns>bool</returns> public static bool ValidateUser(Logon logon, HttpResponseBase response) { bool result = false; if (Membership.ValidateUser(logon.Username, logon.Password)) { // Create the authentication ticket with custom user data. var serializer = new JavaScriptSerializer(); string userData = serializer.Serialize(UserManager.User); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, logon.Username, DateTime.Now, DateTime.Now.AddDays(30), true, userData, FormsAuthentication.FormsCookiePath); // Encrypt the ticket. string encTicket = FormsAuthentication.Encrypt(ticket); //encTicket = ZipLib.Zip(encTicket); // Create the cookie. HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName); cookie.Expires = DateTime.Now.AddDays(1); cookie.Value = encTicket; response.AppendCookie(cookie); //response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)); result = true; } return result; }