Example #1
0
        public async Task <IActionResult> Register([FromBody] RegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var dbUser = await _userRepository.GetByUsername(model.Mobile);

            if (dbUser != null)
            {
                ModelState.AddModelError("", ErrorMessageResource.UserAlreadyRegistered);
                return(BadRequest(ModelState));
            }
            var user = new User
            {
                FirstName            = model.FirstName,
                LastName             = model.LastName,
                UserName             = model.Mobile,
                CreateDate           = DateTime.Now,
                PhoneNumber          = model.Mobile,
                PhoneNumberConfirmed = false
            };

            var result = await _userManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                ModelState.AddIdentityResultErrors(result);
                return(BadRequest(result.Errors));
            }

            return(Ok());
        }
Example #2
0
        public async Task <IActionResult> ChangePassword([FromBody] ChangePasswordModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = HttpContext.User;

            if (user == null)
            {
                return(BadRequest(string.Format(ErrorMessageResource.NotFoundError, DisplayNameResource.User)));
            }

            var currentUser = await _userManager.GetUserAsync(user);

            var result = await _userManager.ChangePasswordAsync(currentUser, model.OldPassword, model.NewPassword);

            if (!result.Succeeded)
            {
                ModelState.AddIdentityResultErrors(result);
                return(BadRequest(ModelState));
            }

            return(Ok());
        }
Example #3
0
        public async Task <IActionResult> Delete(UserViewModel viewModel)
        {
            IdentityResult result = await _userManager.DeleteApplicationUserAsync(viewModel.UserName);

            ModelState.AddIdentityResultErrors(result);
            if (result.Succeeded)
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                return(View(viewModel));
            }
        }
Example #4
0
        public async Task <IActionResult> Register([FromBody] SaveUserResource userResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userIdentity = mapper.Map <ApplicationUser>(userResource);

            userIdentity.DateRegistered = DateTime.Now;

            var result = await userManager.CreateAsync(userIdentity, userResource.Password);

            if (!result.Succeeded)
            {
                return(BadRequest(ModelState.AddIdentityResultErrors(result)));
            }

            return(Ok(mapper.Map <PlainUserResource>(userIdentity)));
        }
Example #5
0
        public async Task <IActionResult> ResetPassword([FromBody] ResetPasswordModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await _userManager.FindByNameAsync(model.Mobile);

            if (user == null)
            {
                return(BadRequest(string.Format(ErrorMessageResource.NotFoundError, DisplayNameResource.User)));
            }

            var result = await _userManager.ResetPasswordAsync(user, model.Code, model.Password);

            if (!result.Succeeded)
            {
                ModelState.AddIdentityResultErrors(result);
                return(BadRequest(ModelState));
            }

            return(Ok());
        }