Esempio n. 1
0
        public ActionResult Register(RegisterModel registerModel)
        {
            if (ModelState.IsValid)
            {
                User user = _blogRepository.ListUsers.Where(u => u.Email == registerModel.Email && u.Password == registerModel.Password).FirstOrDefault();
                if (user == null)
                {
                    user = new User { Email = registerModel.Email, Password = registerModel.Password, Name = registerModel.Name };
                    _blogRepository.AddUser(user);
                    user = _blogRepository.ListUsers.Where(u => u.Email == registerModel.Email && u.Password == registerModel.Password).FirstOrDefault();
                    if (user != null)
                    {
                        FormsAuthentication.SetAuthCookie(registerModel.Email, true);
                        return RedirectToAction("SuccessRegister", "Account");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Пользователь с таким логином уже существует");
                }
            }

            return View(registerModel);
        }
Esempio n. 2
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    _userService.Register(new User() { UserName = model.UserName, Password = model.Password, Email = model.Email });
                }
                catch (Domain.DomainException ex)
                {
                    foreach (string key in ex.Errors.Keys)
                    {
                        ModelState.AddModelError(key, ex.Errors[key]);
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("DataBaseError", ex.Message);
                }
                if (ModelState.IsValid)
                {
                    ViewBag.Message = "注册成功。";
                    return Login();
                }
            }

            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);
                    }
                }
            }
            return View();
        }