public async Task <IActionResult> DeleteUser([FromRoute] int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await _context.User.SingleOrDefaultAsync(m => m.Id == id); if (user == null) { return(NotFound()); } _context.User.Remove(user); await _context.SaveChangesAsync(); return(Ok(user)); }
public async Task <IActionResult> PostChangePassword([FromBody] PasswordModel passwordModel) { if (!ModelState.IsValid) { return(BadRequest("Invalid request")); } var user = await _context.User.SingleOrDefaultAsync(m => m.Email.Equals(passwordModel.Email)); if (user == null) { return(NotFound("Invalid user")); } if (_crypto.HashMD5(passwordModel.OldPassword) != user.Password) { return(BadRequest("Old password is incorrect")); } user.Password = _crypto.HashMD5(passwordModel.NewPassword); if (user.ForcePasswordChange) { user.ForcePasswordChange = false; } await _context.SaveChangesAsync(); var pathToFile = _env.ContentRootPath + Path.DirectorySeparatorChar.ToString() + "EmailTemplates" + Path.DirectorySeparatorChar.ToString() + "ChangePasswordTemplate.html"; StreamReader SourceReader = System.IO.File.OpenText(pathToFile); string htmlBody = SourceReader.ReadToEnd(); SourceReader.Close(); Email emailContent = new Email() { To = passwordModel.Email, Subject = "Password Change", Body = string.Format(htmlBody, user.Name) }; await emailService.SendEmail(emailConfig, emailContent); return(Ok("Password updated successfully")); }