public async Task <IHttpActionResult> RequestPasswordReset([FromBody] string email) { _logger.Debug(string.Format("Begin. Email: [{0}]", email)); var user = await NdUserManager.FindByEmailAsync(email); if (user == null) { _logger.Debug(string.Format("User was not found. Email: [{0}]", email)); return(NotFound()); } try { string code = await NdUserManager.GeneratePasswordResetTokenAsync(user.Id); var callbackUrl = new Uri(Url.Link("ResetPasswordRoute", new { userId = user.Id, code = code })); await NdUserManager.SendEmailAsync(user.Id, "Reset Password", NdEmailService.CreateResetPasswordBody(callbackUrl.ToString())); } catch (Exception ex) { _logger.Error(string.Format("Error sending ResetPassword email for user. Email: [{0}]", email), ex); return(InternalServerError(ex)); } _logger.Debug(string.Format("Reset password email sent succeessfully. Email: [{0}]", email)); return(Ok()); }
public async Task <IHttpActionResult> ResetPassword(ResetPasswordDto resetPasswordDto) { _logger.Debug(string.Format("Begin. Code: [{0}], Id: [{1}]", resetPasswordDto.Code, resetPasswordDto.Id)); if (!ModelState.IsValid) { _logger.Error(string.Format( "Model state is not valid. ModelState: [{0}]", string.Join(Environment.NewLine, ModelState.Select(x => string.Format("{0}: {1}", x.Key, x.Value))))); return(BadRequest(ModelState)); } IdentityResult result = await NdUserManager.ResetPasswordAsync(resetPasswordDto.Id, resetPasswordDto.Code, resetPasswordDto.Password); if (result.Succeeded) { _logger.Debug(string.Format("Password reseted successfully. Id: [{0}]", resetPasswordDto.Id)); try { await NdUserManager.SendEmailAsync(resetPasswordDto.Id, "Password Reseted", NdEmailService.CreatePasswordResetedBody()); } catch (Exception ex) { _logger.Error(string.Format("Error sending PasswordReseted email. Id: [{0}]", resetPasswordDto.Id), ex); return(InternalServerError(ex)); } return(Ok()); } else { _logger.Error(string.Format( "Reset password failed. Id: [{0}], Code: [{1}], Reason: [{2}]", resetPasswordDto.Id, resetPasswordDto.Code, string.Join(Environment.NewLine, result.Errors))); return(GetErrorResult(result)); } }
public async Task <IHttpActionResult> CreateTherapist(CreateTherapistDto createTherapistDto) { _logger.Debug(string.Format("Begin. Clinic: [{0}], Email: [{1}], FirstName: [{2}], Gender: [{3}], LastName: [{4}], PhoneNumber: [{5}], Title: [{6}]", createTherapistDto.Clinic, createTherapistDto.Email, createTherapistDto.FirstName, createTherapistDto.Gender.ToString(), createTherapistDto.LastName, createTherapistDto.PhoneNumber, createTherapistDto.Title.ToString())); if (!ModelState.IsValid) { _logger.Error(string.Format( "Model state is not valid. ModelState: [{0}]", string.Join(Environment.NewLine, ModelState.Select(x => string.Format("{0}: {1}", x.Key, x.Value))))); return(BadRequest(ModelState)); } var user = new NdUser() { Clinic = createTherapistDto.Clinic, Email = createTherapistDto.Email, FirstName = createTherapistDto.FirstName, Gender = createTherapistDto.Gender, LastName = createTherapistDto.LastName, PhoneNumber = createTherapistDto.PhoneNumber, Title = createTherapistDto.Title, UserName = createTherapistDto.Email }; var password = PasswordGenerator.Generate(); IdentityResult addUserResult = await NdUserManager.CreateAsync(user, password); if (!addUserResult.Succeeded) { _logger.Error(string.Format( "Create user failed. Email: [{0}], Reason: [{1}]", createTherapistDto.Email, string.Join(Environment.NewLine, addUserResult.Errors))); return(GetErrorResult(addUserResult)); } IdentityResult addUserToRoleResult = await NdUserManager.AddToRoleAsync(user.Id, "Therapist"); if (!addUserToRoleResult.Succeeded) { _logger.Error(string.Format( "Add user to roles failed. Email: [{0}], Reason: [{1}]", createTherapistDto.Email, string.Join(Environment.NewLine, addUserResult.Errors))); return(GetErrorResult(addUserResult)); } try { Directory.CreateDirectory(HttpContext.Current.Server.MapPath(Path.Combine("~/Results", user.Id))); } catch (Exception ex) { _logger.Error(string.Format("Error creating folder for therapist. Email: [{0}]", createTherapistDto.Email), ex); return(InternalServerError(ex)); } try { string code = await NdUserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code })); await NdUserManager.SendEmailAsync(user.Id, "Confirm your account", NdEmailService.CreateConfirmEmailWithPasswordBody(callbackUrl.ToString(), password)); } catch (Exception ex) { _logger.Error(string.Format("Error sending ConfirmEmail email for therapist. Email: [{0}]", createTherapistDto.Email), ex); return(InternalServerError(ex)); } Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id })); return(Created(locationHeader, _factory.Create(user))); }
public async Task <IHttpActionResult> UpdateMyInfo(UserInfoDto myInfoDto) { _logger.Debug(string.Format("Begin. Clinic: [{0}], Email: [{1}], FirstName: [{2}], Gender: [{3}], LastName: [{4}], PhoneNumber: [{5}], Title: [{6}]", myInfoDto.Clinic, myInfoDto.Email, myInfoDto.FirstName, myInfoDto.Gender.HasValue ? myInfoDto.Gender.Value.ToString() : string.Empty, myInfoDto.LastName, myInfoDto.PhoneNumber, myInfoDto.Title.HasValue ? myInfoDto.Title.Value.ToString() : string.Empty)); var id = User.Identity.GetUserId(); var user = await NdUserManager.FindByIdAsync(id); var emailChanged = false; try { if (!string.IsNullOrWhiteSpace(myInfoDto.Clinic) && user.Clinic != myInfoDto.Clinic) { user.Clinic = myInfoDto.Clinic; } if (!string.IsNullOrWhiteSpace(myInfoDto.Email) && user.Email != myInfoDto.Email) { emailChanged = true; user.EmailConfirmed = false; user.Email = myInfoDto.Email; } if (!string.IsNullOrWhiteSpace(myInfoDto.FirstName) && user.FirstName != myInfoDto.FirstName) { user.FirstName = myInfoDto.FirstName; } if (myInfoDto.Gender.HasValue && user.Gender != myInfoDto.Gender.Value) { user.Gender = myInfoDto.Gender.Value; } if (!string.IsNullOrWhiteSpace(myInfoDto.LastName) && user.LastName != myInfoDto.LastName) { user.LastName = myInfoDto.LastName; } if (!string.IsNullOrWhiteSpace(myInfoDto.PhoneNumber) && user.PhoneNumber != myInfoDto.PhoneNumber) { user.PhoneNumber = myInfoDto.PhoneNumber; } if (myInfoDto.Title.HasValue && user.Title != myInfoDto.Title.Value) { user.Title = myInfoDto.Title.Value; } } catch (Exception ex) { _logger.Error(string.Format("Update my info failed. Id: [{0}]", id), ex); return(InternalServerError(ex)); } var result = await NdUserManager.UpdateAsync(user); if (!result.Succeeded) { _logger.Error(string.Format( "Update my info failed. Email: [{0}], Reason: [{1}]", user.Email, string.Join(Environment.NewLine, result.Errors))); return(GetErrorResult(result)); } await NdDbContext.SaveChangesAsync(); try { await NdUserManager.SendEmailAsync(id, "Account Information Chaged", NdEmailService.CreateAccountInformationChangedBody(_factory.CreateUserInfo(user))); } catch (Exception ex) { _logger.Error(string.Format("Error sending AccountInformationChanged email. Email: [{0}]", user.Email), ex); return(InternalServerError(ex)); } if (emailChanged) { try { string code = await NdUserManager.GenerateEmailConfirmationTokenAsync(id); var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = id, code = code })); await NdUserManager.SendEmailAsync(id, "Confirm your account", NdEmailService.CreateConfirmEmailBody(callbackUrl.ToString())); } catch (Exception ex) { _logger.Error(string.Format("Error sending ConfirmEmail email. Email: {0}]", user.Email), ex); return(InternalServerError(ex)); } } _logger.Debug(string.Format("My info updated. Id: [{0}]", id)); return(Ok()); }