Beispiel #1
0
        public void PasswordsCheckTest()
        {
            PasswordSaltService passwordSaltService = new PasswordSaltService();
            var customer = service.FindByCustomerId(1);
            var result   = passwordSaltService.PasswordsCheck(customer, "0312958");

            Assert.IsTrue(result == true);
        }
Beispiel #2
0
        public ActionResult Login(loginModel model)
        {
            var service             = new CustomerService();
            var passwordSaltService = new PasswordSaltService();
            var customer_list       = service.GetAll().ToList();

            if (customer_list.Any((x) => x.Account == model.User) == false)
            {
                return(RedirectToAction("Login"));
            }
            ;
            if (model.Password == "******")
            {
                return(RedirectToAction("Login"));
            }

            if (passwordSaltService.PasswordsCheck(service.FindByCustomerAccount(model.User), model.Password))
            {
                FormsAuthentication.SignOut();

                var cookie = Request.Cookies[FormsAuthentication.FormsCookieName];

                if (cookie != null)
                {
                    cookie.Expires = DateTime.Now;
                    Response.Cookies.Add(cookie);
                }

                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, model.User, DateTime.Now, DateTime.Now.AddMinutes(30), false, "abcdefg");

                var ticketData = FormsAuthentication.Encrypt(ticket);
                cookie         = new HttpCookie(FormsAuthentication.FormsCookieName, ticketData);
                cookie.Expires = ticket.Expiration; //設定Cookie到期日與憑證同時

                Response.Cookies.Add(cookie);

                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                ModelState.AddModelError("loginModel", "Error");
                return(RedirectToAction("Login", "Login"));
            }
        }