Beispiel #1
0
        public async Task <IActionResult> DeleteUser([FromRoute] int id)
        {
            // Find the user to delete in the database
            var user = await db.Users.FindAsync(id);

            if (user == null)
            {
                return(NotFound(new { message = "User not found." }));
            }

            // Verify that the currently authenticated user is the same as the user who we're deleting
            var authUser = Int32.Parse(HttpContext.User.Identity.Name);

            if (user.UserId != authUser || id != authUser)
            {
                return(Unauthorized(new { message = "You are not authorized to delete that user account." }));
            }

            // Remove the user from the database & save the changes
            db.Remove(user);
            await db.SaveChangesAsync();

            return(Ok(new { message = "User account successfully deleted." }));
        }