コード例 #1
0
        //
        // GET: /Account/Manage
        public ActionResult Manage(ManageMessageId? message)
        {
            ViewBag.StatusMessage =
                message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
                : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
                : message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed."
                : message == ManageMessageId.Error ? "An error has occurred."
                : "";

            ViewBag.HasLocalPassword = HasPassword();
            ViewBag.ReturnUrl = Url.Action("Manage");

            var user = UserManager.FindById(User.Identity.GetUserId());
            var model = new ManageUserViewModel
            {           
                userInfoVM = new ManageUserInfoViewModel
                {
                    Email = user.Email,
                    Alias = user.Alias
                }   
            };
            
            return View(model);
        }
コード例 #2
0
        public async Task<ActionResult> Manage(ManageUserViewModel model)
        {
            bool hasPassword = HasPassword();
            ViewBag.HasLocalPassword = hasPassword;
            ViewBag.ReturnUrl = Url.Action("Manage");
            ViewBag.DisplayPasswordModal = false;

            if (model.userInfoVM != null)
            {
                if (ModelState.IsValid)
                {
                    using (ApplicationDbContext context = new ApplicationDbContext())
                    {
                        ICollection<NewsFeed> newsFeeds = new List<NewsFeed>();
                        ApplicationUser user = context.Users.Where(u => u.Email == model.userInfoVM.Email).SingleOrDefault();

                        user.Email = model.userInfoVM.Email;
                        user.Alias = model.userInfoVM.Alias;

                        ViewBag.StatusMessage = "Info successfully updated!";

                        await context.SaveChangesAsync();
                    }
                }
            }

            if (model.userPasswordVM != null)
            {
                if (hasPassword)
                {
                    if (ModelState.IsValid)
                    {
                        if (model.userPasswordVM.OldPassword != null && model.userPasswordVM.OldPassword != String.Empty)
                        {
                            IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.userPasswordVM.OldPassword, model.userPasswordVM.NewPassword);
                            if (result.Succeeded)
                            {
                                ViewBag.DisplayPasswordModal = false;

                                var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
                                await SignInAsync(user, isPersistent: false);
                                return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
                            }
                            else
                            {
                                ViewBag.DisplayPasswordModal = true;
                                AddErrors(result);
                            }
                        }
                    }
                }
                else
                {
                    // User does not have a password so remove any validation errors caused by a missing OldPassword field
                    ModelState state = ModelState["OldPassword"];
                    if (state != null)
                    {
                        state.Errors.Clear();
                    }

                    if (ModelState.IsValid)
                    {
                        IdentityResult result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.userPasswordVM.NewPassword);
                        if (result.Succeeded)
                        {
                            ViewBag.DisplayPasswordModal = false;
                            return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess });
                        }
                        else
                        {
                            ViewBag.DisplayPasswordModal = true;
                            AddErrors(result);
                        }
                    }
                }
            }              

            // If we got this far, something failed, redisplay form
            return View(model);
        }   
コード例 #3
0
        public async Task<ActionResult> Manage(ManageUserViewModel model)
        {
            bool hasPassword = HasPassword();
            ViewBag.HasLocalPassword = hasPassword;
            ViewBag.ReturnUrl = Url.Action("Manage");
            if (hasPassword)
            {
                if (ModelState.IsValid)
                {
                    IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);
                    if (result.Succeeded)
                    {
                        var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
                        await SignInAsync(user, isPersistent: false);
                        return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }
            else
            {
                // User does not have a password so remove any validation errors caused by a missing OldPassword field
                ModelState state = ModelState["OldPassword"];
                if (state != null)
                {
                    state.Errors.Clear();
                }

                if (ModelState.IsValid)
                {
                    IdentityResult result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);
                    if (result.Succeeded)
                    {
                        return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess });
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }

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