コード例 #1
0
 public ActionResult Register(UserProfileAndRegisterModel model)
 {
     if (ModelState.IsValid)
     {
         // Attempt to register the user
         try
         {
             WebSecurity.CreateUserAndAccount(model.ARegisterModel.UserName, model.ARegisterModel.Password);
             WebSecurity.Login(model.ARegisterModel.UserName, model.ARegisterModel.Password);
             //Save the mail for the user created
             UserProfile userModel = new UserProfile();
             using (UsersContext db = new UsersContext())
             {
                 userModel = model.AUserProfile;
                 userModel.UserId = WebSecurity.GetUserId(model.ARegisterModel.UserName);
                 userModel.UserName = model.ARegisterModel.UserName;
                 try
                 {
                     db.Entry(userModel).State = EntityState.Modified;
                     db.SaveChanges();
                 }
                 catch (Exception)
                 {
                     Membership.DeleteUser(model.ARegisterModel.UserName);
                     WebSecurity.Logout();
                     return RedirectToAction("Index", "Home");
                 }
             }
             return RedirectToAction("Index", "Home");
         }
         catch (MembershipCreateUserException e)
         {
             ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
         }
     }
     // If we got this far, something failed, redisplay form
     return View(model);
 }
コード例 #2
0
        public ActionResult Manage(UserProfile model)
        {
            bool hasLocalAccount = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));
            ViewBag.HasLocalPassword = hasLocalAccount;
            ViewBag.ReturnUrl = Url.Action("Manage");
            if (hasLocalAccount)
            {
                if (ModelState.IsValid)
                {
                // Attempt to update user data
                try
                {
                    using (UsersContext db = new UsersContext())
                    {
                        UserProfile aUser = new UserProfile();
                        model.UserId = WebSecurity.GetUserId(User.Identity.Name);
                        model.UserName = User.Identity.Name;
                        bool saveDataSuceeded=true;
                        try
                        {
                            db.Entry(model).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                        catch (Exception)
                        {
                            saveDataSuceeded = false;
                        }

                        if (saveDataSuceeded)
                        {
                            return RedirectToAction("Manage", new { Message = ManageMessageId.DataSaveSuccess });
                        }
                    }
                    return RedirectToAction("Manage", "Account");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
                }
            }

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