Esempio n. 1
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // 尝试注册用户
                MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                }
            }

            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
            return View(model);
        }
Esempio n. 2
0
        public ActionResult Register(RegisterModel newUserModel)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                var newUser = Membership.CreateUser(newUserModel.UserName, newUserModel.Password, newUserModel.Email, null, null, true, null, out createStatus);
                if (createStatus == MembershipCreateStatus.Success)
                {
                    SimPresEntities db = new SimPresEntities();
                    db.Users.AddObject(
                        new User()
                        {
                            UserId = (Guid)newUser.ProviderUserKey,
                            Login = newUserModel.UserName,
                            About = newUserModel.About,
                            Email = newUserModel.Email,
                            RegistrationDate = DateTime.Now
                        });
                    //Фиксируем изменения в БД
                    try
                    {
                        db.SaveChanges();
                        FormsAuthentication.SetAuthCookie(newUserModel.UserName, false);
                        return RedirectToAction("Index", "Home");
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError("", ex);
                    }
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(newUserModel);
        }