public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    // Create a profile, password, and link the local login before signing in the user
                    User user = new User(model.UserName);
                    if (await Users.Create(user) &&
                        await Secrets.Create(new UserSecret(model.UserName, model.Password)) &&
                        await Logins.Add(new UserLogin(user.Id, IdentityConfig.LocalLoginProvider, model.UserName)))
                    {
                        await SignIn(user.Id, isPersistent : false);

                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        ModelState.AddModelError(String.Empty, "Failed to create login for: " + model.UserName);
                    }
                }
                catch (DbEntityValidationException e)
                {
                    ModelState.AddModelError("", e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage);
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public async Task <ActionResult> Manage(ManageUserViewModel model)
        {
            string userId        = User.Identity.GetUserId();
            string localUserName = await Logins.GetProviderKey(User.Identity.GetUserId(), IdentityConfig.LocalLoginProvider);

            bool hasLocalLogin = localUserName != null;

            ViewBag.HasLocalPassword = hasLocalLogin;
            ViewBag.ReturnUrl        = Url.Action("Manage");
            if (hasLocalLogin)
            {
                if (ModelState.IsValid)
                {
                    bool changePasswordSucceeded = await ChangePassword(localUserName, model.OldPassword, model.NewPassword);

                    if (changePasswordSucceeded)
                    {
                        return(RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess }));
                    }
                    else
                    {
                        ModelState.AddModelError(String.Empty, "The current password is incorrect or the new password is invalid.");
                    }
                }
            }
            else
            {
                // User does not have a local 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)
                {
                    try
                    {
                        // Create the local login info and link the local account to the user
                        localUserName = User.Identity.GetUserName();
                        if (await Secrets.Create(new UserSecret(localUserName, model.NewPassword)) &&
                            await Logins.Add(new UserLogin(userId, IdentityConfig.LocalLoginProvider, localUserName)))
                        {
                            return(RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess }));
                        }
                        else
                        {
                            ModelState.AddModelError(String.Empty, "Failed to set password");
                        }
                    }
                    catch (Exception e)
                    {
                        ModelState.AddModelError(String.Empty, e);
                    }
                }
            }

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