示例#1
0
        public ActionResult Login(LoginModel model)
        {
            User user = null;
            if (ModelState.IsValid)
            {
                try
                {
                    user = _userService.Login(model.UserName, model.Password);
                }
                catch (Domain.DomainException ex)
                {
                    foreach (string key in ex.Errors.Keys)
                    {
                        ModelState.AddModelError(key, ex.Errors[key]);
                    }
                }
            }

            if (!ModelState.IsValid)
            {
                ViewBag.Errors = new List<string>();
                foreach (ModelState state in ModelState.Values)
                {
                    foreach (ModelError error in state.Errors)
                    {
                        ViewBag.Errors.Add(error.ErrorMessage);
                    }
                }
            }

            if (user != null)
            {
                if (model.RememberMe == true)
                {
                    Response.SetCookie(new HttpCookie("UserName", model.UserName));
                    Response.SetCookie(new HttpCookie("Password", model.Password));
                }
                else
                {
                    Response.SetCookie(new HttpCookie("UserName", string.Empty));
                    Response.SetCookie(new HttpCookie("Password", string.Empty));
                }
                System.Web.Security.FormsAuthentication.SetAuthCookie(user.UserName, false);
                if (string.IsNullOrEmpty(model.ReturnUrl))
                {
                    if (user.Role == UserRole.Admin)
                        return RedirectToAction("Index", "Main", new { area = "Admin" });
                    else
                        return RedirectToAction("Info", "User", new { area = "Admin" });
                }
                else
                    return Redirect(model.ReturnUrl);

            }
            else
            {
                return View("Login");
            }
        }
        public ActionResult Login(LoginModel loginModel)
        {
            if (ModelState.IsValid)
            {
                User user = _blogRepository.ListUsers.Where(u => u.Email == loginModel.Email && u.Password == loginModel.Password).FirstOrDefault();
                if (user != null)
                {
                    FormsAuthentication.SetAuthCookie(loginModel.Email, true);
                    return RedirectToAction("AllBlogs", "Blog");
                }
                else
                {
                    ModelState.AddModelError("", "Пользователя с таким логином и паролем нет");
                }
            }

            return View(loginModel);
        }