コード例 #1
0
        public async Task EditAccountInfo(EditAccountInfoVM model)
        {
            var user = await _userManager.FindByIdAsync(model.Id);

            if (!String.IsNullOrEmpty(model.NewPassword) && !String.IsNullOrEmpty(model.OldPassword))
            {
                if (user != null && await _userManager.CheckPasswordAsync(user, model.OldPassword))
                {
                    var code = await _userManager.GeneratePasswordResetTokenAsync(user);

                    var result = await _userManager.ResetPasswordAsync(user, code, model.NewPassword);

                    if (!result.Succeeded)
                    {
                        throw new Exception("Change password fail");
                    }
                }
                else
                {
                    throw new Exception("User not found or invalid OldPassword");
                }
            }

            var userProfile = _db.UserProfiles.GetAll().First(m => m.UserId == model.Id);

            if (userProfile == null)
            {
                throw new Exception("User profile not found");
            }

            if (!String.IsNullOrEmpty(model.FirstName))
            {
                userProfile.FirstName = model.FirstName;
            }

            if (!String.IsNullOrEmpty(model.LastName))
            {
                userProfile.LastName = model.LastName;
            }

            if (model.SexId != null)
            {
                userProfile.SexId = model.SexId;
            }

            if (model.DateBirth != null)
            {
                userProfile.DateBirth = model.DateBirth.Value;
            }

            var editResult = await _db.UserProfiles.UpdateAsync(userProfile);

            if (editResult == null)
            {
                throw new Exception("Edit user info fail");
            }

            await _emailService.SendEmailAsync(user.Email, "Edit your account info",
                                               $"Your Account Info has been updating.");
        }
コード例 #2
0
        public async Task <IActionResult> EditMyAccountInformation([FromBody] EditAccountInfoVM editUser)
        {
            try
            {
                if (editUser == null)
                {
                    throw new Exception("editUser param is null");
                }
                editUser.Id = User.Claims.First(c => c.Type == "UserID").Value;
                await _accountService.EditAccountInfo(editUser);

                return(Ok());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }