public ActionResult ResetPassword(int id, PatientPasswordViewModel viewModel) { var patient = BVSBusinessServices.Patients.GetByID(id); ApplicationDbContext context = new ApplicationDbContext(); var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); var user = userManager.FindById(patient.MembershipProviderUserID); var result = userManager.RemovePassword(user.Id); if (result.Succeeded) { result = userManager.AddPassword(user.Id, viewModel.NewPassword); } if (result.Succeeded) { return(RedirectToAction("Details", new { id = id })); } else { ModelState.AddModelError("Password", String.Format("Unable to change password. {0}", String.Join(", ", result.Errors))); return(View()); } }
public ActionResult Create(PatientPasswordViewModel viewModel) { BusinessServiceOperationResult result; using (var transaction = new TransactionScope()) { result = BVSBusinessServices.Patients.Save(viewModel.Patient); if (!result.HasErrors) { ApplicationDbContext context = new ApplicationDbContext(); var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); var user = new ApplicationUser { UserName = viewModel.Patient.EmailAddress, Email = viewModel.Patient.EmailAddress }; var identityResult = userManager.Create(user, viewModel.NewPassword); if (!identityResult.Succeeded) { result.AddError(String.Format("Could not create application user account: {0}", String.Join(", ", identityResult.Errors))); } else { identityResult = userManager.AddToRole(user.Id, "Patient"); if (!identityResult.Succeeded) { result.AddError("Could not assign user to patient role"); } } if (!result.HasErrors) { viewModel.Patient.MembershipProviderUserID = user.Id; result = BVSBusinessServices.Patients.Save(viewModel.Patient); } if (!result.HasErrors) { transaction.Complete(); } } } if (!result.HasErrors) { return(RedirectToAction("Details", "Patient", new { id = viewModel.Patient.ID })); } else { foreach (var error in result.Errors) { ModelState.AddModelError("key", error); } return(View(viewModel)); } }
public ActionResult ResetPassword(int id) { var viewModel = new PatientPasswordViewModel { Patient = BVSBusinessServices.Patients.GetByID(id) }; return(View(viewModel)); }
public ActionResult Create() { var viewModel = new PatientPasswordViewModel { Patient = new Patient() }; return(View(viewModel)); }