Skip to content

How to custome login and create cookies in mvc 4

Surendra Kandira

Surendra Kandira

Asked Jul 14, 2015, 11:49 PM EDT3 answers
Share
LinkedInX
Custom login and create cookies in mvc 4. Cookies as timeout time and store in encrypt format

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Surendra Kandira
    0
      public ActionResult Index(LoginM model,string returnUrl)
             {
                 ///var macAddress = db.MacAddresses.FirstOrDefault(a => a.Username.Equals(model.UserName) && a.isActive == true);

                if (Membership.ValidateUser(model.UserName, model.Password) && ModelState.IsValid)
                {
                    //if (macAddress != null && !GetMACAddress().Equals(macAddress.MacAddress1.Replace("-","")))
                       // {
                          //  ViewBag.Message = "Wrong You are not allow on this system, Try again another system";
                        //}
                    //else {

                    int id = Convert.ToInt32(model.UserName);
                    var prefence = new Preference();
                    prefence = db.Preferences.Where(x => x.EmployeeId == id).FirstOrDefault();
                    HttpCookie cookie = Request.Cookies["DropOrder"];
                    if (model.RememberMe == true)
                    {
                        int timeout = model.RememberMe ? 525600 : 30;
                        var ticket = new FormsAuthenticationTicket(model.UserName, model.RememberMe, timeout);
                        string encrypted = FormsAuthentication.Encrypt(ticket);
                        var cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
                        cookie1.Expires = System.DateTime.Now.AddMinutes(timeout);//My Line
                        cookie1.HttpOnly = true;
                        HttpContext.Response.Cookies.Add(cookie1);
                        //FormsAuth.SetAuthCookie
                    }
                    var response = HttpContext.Response;
                    if (Request.Cookies["DropOrder"] != null)
                    {
                        response.Cookies.Remove("DropOrder");
                    }
                    string str = "";
                    if (prefence == null)
                    {
                        var prefence1 = new Preference();
                        prefence1.EmployeeId = id;
                        prefence1.OrderPreference = "orderName";
                        db.Preferences.Add(prefence1);
                        db.SaveChanges();
                        str = "orderName";
                    }
                    else
                    {
                        str = prefence.OrderPreference;
                    }
                    var DropOrder = new HttpCookie("DropOrder");

                    DropOrder.Value = str;
                    DropOrder.Expires = DateTime.Now.AddDays(7);
                    Response.Cookies.Add(DropOrder);

                    FormsAuthentication.SetAuthCookie(model.UserName, false);
                    if (Url.IsLocalUrl(returnUrl))
                        return Redirect(returnUrl);
                    else
                        return RedirectToAction("Users", "Home");
                }
                //}
                else
                {
                    ViewBag.Message = "Wrong UserName/Password, Try again";
                }
                return View();

            }
    0
  • 0
    Can you post front end logic too.. it will very useful
     
    Thank you 
    0
  • Surendra Kandira
    0
    http://www.codeproject.com/Articles/322154/ASP-NET-MVC-SignalR-and-Knockout-based-real-time-U
    0