public async Task <IActionResult> DeleteAccount(DeleteAccountInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }
            var user = await this.userManager.GetUserAsync(this.User);

            if (user.Email == input.Email && user.FirstName == input.FirstName && user.LastName == input.LastName)
            {
                await this.signInManager.SignOutAsync();

                await this.userManager.RemoveFromRoleAsync(user, GlobalConstants.UserRoleName);

                await this.myAccountService.DeleteAccountDataAsync(user.Id);

                await this.userManager.DeleteAsync(user);

                return(this.Redirect("/"));
            }
            else
            {
                return(this.View(input));
            }
        }
        public async Task <IActionResult> Delete(DeleteAccountInputModel input)
        {
            var loggedInUserName = User.FindFirst(ClaimTypes.Name).Value;
            var user             = await _userManager.FindByNameAsync(loggedInUserName);

            if (user == null)
            {
                return(BadRequest("User doesn't exist."));
            }

            var isValidPassword = await _userManager.CheckPasswordAsync(user, input.Password);

            if (!isValidPassword)
            {
                return(BadRequest("Incorrect Password."));
            }

            var result = await _userManager.DeleteAsync(user);

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to delete user account. Please try again."));
            }

            // sign-out if account deleted successfully
            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

            return(Ok("Account deleted successfully."));
        }
Example #3
0
        public async Task <IActionResult> AccountDelete(DeleteAccountInputModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await userManager.GetUserAsync(User);

                if (user == null)
                {
                    return(NotFound($"Unable to find user. Please contact administrator."));
                }

                await signInManager.SignOutAsync();

                await userManager.DeleteAsync(user);
            }

            return(LocalRedirect("/"));
        }
        public async Task <IActionResult> DeleteAccount(DeleteAccountInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var user = await this.userManager.GetUserAsync(this.User);

            if (user.Email == input.Email)
            {
                await this.signInManager.SignOutAsync();

                await this.userManager.DeleteAsync(user);

                return(this.Redirect("/"));
            }
            else
            {
                return(this.View(input));
            }
        }