public void CookiesANDRememberMe() { // Set Cookies if the RememberMe is checked. if (RememberMe.Checked) { String UserName = Request.Form["ctl00$MainContent$UserName"]; // Create Authentication Cookies. var authTicket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddDays(Convert.ToInt32(ConfigurationManager.ConnectionStrings["CookieSaveDay"].ConnectionString)), true, "", "/"); //Encrypt the ticket and add it to the cookie. HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket)); cookie.Expires = DateTime.Now.AddDays(Convert.ToInt32(ConfigurationManager.ConnectionStrings["CookieSaveDay"].ConnectionString)); Response.Cookies.Add(cookie); // Create a new instance of Cookies class. cookies cookiez = new cookies(); // Create MacAddressCookie HttpCookie MacAddressCookie = new HttpCookie(ConfigurationManager.ConnectionStrings["MacCookieName"].ConnectionString); MacAddressCookie.Value = cookiez.Protect(cookiez.MacAddress, ConfigurationManager.ConnectionStrings["MacCookieName"].ConnectionString); MacAddressCookie.Expires = DateTime.Now.AddDays(Convert.ToInt32(ConfigurationManager.ConnectionStrings["CookieSaveDay"].ConnectionString)); Response.Cookies.Add(MacAddressCookie); // Create IpAddressCookie HttpCookie IpAddressCookie = new HttpCookie(ConfigurationManager.ConnectionStrings["IpCookieName"].ConnectionString); IpAddressCookie.Value = cookiez.Protect(cookiez.IPAddress, ConfigurationManager.ConnectionStrings["IpCookieName"].ConnectionString); IpAddressCookie.Expires = DateTime.Now.AddDays(Convert.ToInt32(ConfigurationManager.ConnectionStrings["CookieSaveDay"].ConnectionString)); Response.Cookies.Add(IpAddressCookie); } }
protected void Page_Load(object sender, EventArgs e) { if (Request.UrlReferrer != null) { if (Request.UrlReferrer.ToString().ToLower().Contains("checkin.aspx")) { checkInDay = true; } } if (!IsPostBack) { InitializeForm(); HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; HttpCookie macCookie = Request.Cookies[ConfigurationManager.ConnectionStrings["MacCookieName"].ConnectionString]; HttpCookie ipCookie = Request.Cookies[ConfigurationManager.ConnectionStrings["IpCookieName"].ConnectionString]; if (authCookie != null && macCookie != null && ipCookie != null) { // Get the Form Authentcation cookie. FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value); // Check to see if Authentication cookie has been exired or not. if (!ticket.Expired) { cookies decryptCookie = new cookies(); if (decryptCookie.Unprotect(macCookie.Value, ConfigurationManager.ConnectionStrings["MacCookieName"].ConnectionString) == decryptCookie.MacAddress && decryptCookie.Unprotect(ipCookie.Value, ConfigurationManager.ConnectionStrings["IpCookieName"].ConnectionString) == decryptCookie.IPAddress) { SessionVariables.UserName = ticket.Name; Response.Redirect("/Default.aspx"); } } } } ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:disableSubmit(); ", true); }
protected void Page_Load(object sender, EventArgs e) { if (Request.UrlReferrer != null) { if (!Request.UrlReferrer.ToString().Contains("Account/Login.aspx")) { redirect = Request.UrlReferrer.ToString(); } } if (!IsPostBack) { ErrorString = null; HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; HttpCookie macCookie = Request.Cookies[ConfigurationManager.ConnectionStrings["MacCookieName"].ConnectionString]; HttpCookie ipCookie = Request.Cookies[ConfigurationManager.ConnectionStrings["IpCookieName"].ConnectionString]; if (authCookie != null && macCookie != null && ipCookie != null) { // Get the Form Authentcation cookie. FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value); // Check to see if Authentication cookie has been exired or not. if (!ticket.Expired) { cookies decryptCookie = new cookies(); if (decryptCookie.Unprotect(macCookie.Value, ConfigurationManager.ConnectionStrings["MacCookieName"].ConnectionString) == decryptCookie.MacAddress && decryptCookie.Unprotect(ipCookie.Value, ConfigurationManager.ConnectionStrings["IpCookieName"].ConnectionString) == decryptCookie.IPAddress) { SessionVariables.UserName = ticket.Name; if (String.IsNullOrEmpty(redirect)) { Response.Redirect("/Default.aspx"); } else if (redirect.Contains("/AccountManagement.aspx")) { Response.Redirect("/Default.aspx"); } else { Response.Redirect(redirect); } } } } } else { } }
public bool UserCookiesExists() { HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; HttpCookie macCookie = Request.Cookies[ConfigurationManager.ConnectionStrings["MacCookieName"].ConnectionString]; HttpCookie ipCookie = Request.Cookies[ConfigurationManager.ConnectionStrings["IpCookieName"].ConnectionString]; if (authCookie != null && macCookie != null && ipCookie != null) { // Get the Form Authentcation cookie. FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value); // Check to see if Authentication cookie has been exired or not. if (!ticket.Expired) { cookies decryptCookie = new cookies(); if (decryptCookie.Unprotect(macCookie.Value, ConfigurationManager.ConnectionStrings["MacCookieName"].ConnectionString) == decryptCookie.MacAddress && decryptCookie.Unprotect(ipCookie.Value, ConfigurationManager.ConnectionStrings["IpCookieName"].ConnectionString) == decryptCookie.IPAddress) { return(true); } } } return(false); }