Esempio n. 1
0
        public ActionResult Login(UserEntity u)
        {
            if (u.Email != null && u.Password != null)
            {
                var user = _userservice.GetUserByName(u.Email, u.Password);
                if (user != null)
                {
                    FormsAuthentication.SetAuthCookie(u.Email, u.Rememberme);
                    if (u.Rememberme)
                    {
                        cookie.Values.Add("Email", user.Email);
                        cookie.Values.Add("Password", u.Password);
                        cookie.Values.Add("Rememberme", (u.Rememberme).ToString());
                        cookie.Expires = DateTime.Now.AddDays(15);
                        Response.Cookies.Add(cookie);
                        //var createPersistentCookie = u.Rememberme;
                        //int timeout = createPersistentCookie ? 525600 : 2; // Timeout in minutes,525600 = 365 days
                        //var ticket = new FormsAuthenticationTicket(u.Email, createPersistentCookie, timeout);
                        //string encrypted = FormsAuthentication.Encrypt(ticket);
                        //var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
                        //cookie.Expires = System.DateTime.Now.AddMinutes(timeout);//My Line
                        //HttpContext.Response.Cookies.Add(cookie);

                        // FormsAuthentication.SetAuthCookie(u.Email, false);
                        //var authTicket = new FormsAuthenticationTicket(1, u.Email, DateTime.Now, DateTime.Now.AddMinutes(20), false, "Testing");
                        //string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                        //var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                        //HttpContext.Response.Cookies.Add(authCookie);
                    }
                    else
                    {
                        Response.Cookies["Login"].Values["Rememberme"] = "false";
                        //cookie.Values.Remove(Convert.ToBoolean(Request.Cookies["Login"].Values["Rememberme"]).ToString());
                    }

                    ApplicationSession.Login(user);
                    Session["Id"]         = user.Id;
                    Session["username"]   = user.FirstName + "  " + user.LastName;
                    Session["rememberme"] = u.Rememberme;
                    //  LoggedInCkeck.g_IsLoggedIn = Session["IsLoggedIn"] == null ? false : (bool)Session["IsLoggedIn"];
                    return(RedirectToAction("Index", "Dashboard"));
                }
                else
                {
                    ModelState.AddModelError("", "Invalid User Name or Password ");
                }
            }
            return(View(u));
        }