Exemple #1
0
        public async Task <ActionResult> AccountInfo(User userUpdate)
        {
            //Get logged in user
            var userId      = UserMgr.GetUserId(HttpContext.User);
            var currentUser = UserMgr.FindByIdAsync(userId).Result;

            //Ensure unique email
            string updateEmail = userUpdate.Email;

            if (UserMgr.FindByEmailAsync(updateEmail).Result == null || UserMgr.FindByEmailAsync(updateEmail).Result == currentUser)
            {
                currentUser.Email = userUpdate.Email;
            }
            else
            {
                return(View());
            }

            //Password
            if (userUpdate.Password == null)
            {
                currentUser.Password = currentUser.Password;
            }
            else
            {
                currentUser.Password = userUpdate.Password;
                await UserMgr.RemovePasswordAsync(currentUser);

                await UserMgr.AddPasswordAsync(currentUser, userUpdate.Password);
            }

            //Name
            currentUser.FirstName = userUpdate.FirstName;
            currentUser.LastName  = userUpdate.LastName;

            IdentityResult x = await UserMgr.UpdateAsync(currentUser);

            if (x.Succeeded)
            {
                return(RedirectToAction("Dashboard", "Application"));
            }
            else
            {
                ViewBag.ErrorMessage = "Error";
                return(View(userUpdate));
            }
        }
Exemple #2
0
        public async Task <IActionResult> Update(string field, string value)
        {
            Console.WriteLine(field);
            Console.WriteLine(value);
            var username = User?.Identity.Name;
            var user     = await UserMgr.FindByNameAsync(username);

            if (field == "firstName")
            {
                user.FirstName = value;
            }
            else if (field == "lastName")
            {
                user.LastName = value;
            }
            else if (field == "newPassword")
            {
                await UserMgr.RemovePasswordAsync(user);

                await UserMgr.AddPasswordAsync(user, value);
            }
            else if (field == "newEmail")
            {
                Console.WriteLine(RegexUtilities.IsValidEmail(value));
                if (RegexUtilities.IsValidEmail(value))
                {
                    var checkEmail = await UserMgr.FindByEmailAsync(value);

                    if (checkEmail == null)
                    {
                        await UserMgr.SetEmailAsync(user, value);

                        var newUser = await UserMgr.FindByEmailAsync(value);

                        await SignInMgr.SignOutAsync();

                        await SignInMgr.SignInAsync(newUser, true);

                        user = newUser;
                    }
                    else
                    {
                        return(Ok("Email exists"));
                    }
                }
                else
                {
                    return(Ok("Email invalid"));
                }
            }

            IdentityResult result = await UserMgr.UpdateAsync(user);

            Console.WriteLine(result.Succeeded + "<----");
            if (result.Succeeded)
            {
                var userToSend = new UserModel();
                userToSend.FirstName = user.FirstName;
                userToSend.LastName  = user.LastName;
                userToSend.UserName  = user.NormalizedEmail;
                return(Ok(userToSend));
            }
            return(Ok("Accout update failed"));
        }