コード例 #1
0
        public ActionResult Login(AccountLoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var user = _readOnlyRepository.FirstOrDefault <User>(x => x.Email == model.Email);
                if (user != null)
                {
                    if (PasswordEncryptionService.CheckPassword(user, model.Password))
                    {
                        var ticket = new FormsAuthenticationTicket(1, user.Name, DateTime.Now, DateTime.Now.AddMinutes(30), model.RememberMe, user.Email, FormsAuthentication.FormsCookiePath);

                        // Encrypt the ticket.
                        string encTicket = FormsAuthentication.Encrypt(ticket);

                        // Create the cookie.
                        Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));

                        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") &&
                            !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        {
                            return(Redirect(returnUrl));
                        }
                        return(RedirectToAction("Index", "Synergy"));
                    }
                }
                ModelState.AddModelError("", "The e-mail address or password provided is incorrect.");
            }
            return(View(model));
        }