public async Task <KeyValuePair <int, string> > PostApplicationUser(IdentityUserDTO dto) { dto.UserName = dto.Email; var isUserExixts = await UserExist(dto); if (isUserExixts) { return(new KeyValuePair <int, string>(-3, "User already exists for " + dto.UserName + " email.")); } var user = new ApplicationUser() { UserName = dto.Email, Email = dto.Email, FirstName = dto.FirstName, LastName = dto.LastName }; var result = await _userManager.CreateAsync(user, dto.Password); if (!result.Succeeded) { return(new KeyValuePair <int, string>(-2, result.Errors.FirstOrDefault()?.Description.ToLower())); } var roleRes = await _userManager.AddToRoleAsync(user, Role.WebUser.ToString()); if (!roleRes.Succeeded) { var resDeleteUser = await _userManager.DeleteAsync(user); if (!resDeleteUser.Succeeded) { return(new KeyValuePair <int, string>(-7, "User successfully created but failed to set role, tried to remove user but error occured.")); } return(new KeyValuePair <int, string>(-8, "User successfully created but failed to set role, removed user.")); } var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var fullname = dto.LastName + " " + dto.FirstName; var mailContent = await EmailBodyCreator.CreateConfirmEmailBody(Utilities.GetCurrHost(_httpContext), fullname, dto.UserName, code); try { await _emailService.SendMailAsync(new List <MailAddress>() { new MailAddress(user.Email) }, null, null, AppCommon.AppName + " - Verify Email", mailContent, null); return(new KeyValuePair <int, string>(1, "User successfully created, email sent.")); } catch (Exception ex) { var resDeleteUser = await _userManager.DeleteAsync(user); if (!resDeleteUser.Succeeded) { return(new KeyValuePair <int, string>(-6, "User successfully created but failed to sent email, tried to remove user but error occured.")); } return(new KeyValuePair <int, string>(-4, "User successfully created but failed to sent email, deleted user.")); } }
public async Task <IActionResult> SendEmailAsync(string name, string email) { var mailContent = await EmailBodyCreator.CreateConfirmEmailBody(Utilities.GetCurrHost(_httpContext), name, null, null); await _emailService.SendMailAsync(new List <MailAddress>() { new MailAddress(email, name) }, null, null, AppCommon.AppName + " - Test mail", mailContent, null); return(OKResult(1, "mail sent to " + email)); }
public async Task <KeyValuePair <int, string> > RegisterAsync(RegisterViewModel model) { var isUserExixts = await IsUserExists(model.Email); if (isUserExixts) { return(new KeyValuePair <int, string>(-3, "User already exists for " + model.Email + " email.")); } var user = new ApplicationUser { UserName = model.Email, Email = model.Email, EmailConfirmed = false, FirstName = model.FirstName, LastName = model.LastName }; var resultCreateUser = await _userManager.CreateAsync(user, _appSetting.DefaultPassword); if (!resultCreateUser.Succeeded) { _logger.LogInformation("A new user account " + model.Email + " created with default password " + _appSetting.DefaultPassword); return(new KeyValuePair <int, string>(-2, resultCreateUser.Errors.FirstOrDefault()?.Description.ToLower())); } var roleRes = await _userManager.AddToRoleAsync(user, Role.WebUser.ToString()); if (!roleRes.Succeeded) { _logger.LogError("Couldn't add user to role. " + roleRes.Errors.ToJsonString()); _logger.LogWarning("Deleting user : "******"Couldn't delete user " + user.Email + ", Error(s) : " + roleRes.Errors.ToJsonString()); return(new KeyValuePair <int, string>(-7, "User successfully created but failed to set role, tried to remove user but error occured.")); } return(new KeyValuePair <int, string>(-8, "User successfully created but failed to set role, removed user.")); } var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var fullname = user.FirstName + " " + user.LastName; var mailContent = await EmailBodyCreator.CreateConfirmEmailBody(Utilities.GetCurrHost(_httpContext), fullname, user.Email, code); var fullName = user.FirstName + " " + user.LastName; try { await _emailService.SendMailAsync(new List <MailAddress>() { new MailAddress(user.Email, fullName) }, null, null, AppCommon.AppName + " - Verify Email", mailContent, null); return(new KeyValuePair <int, string>(1, "User successfully created, email sent.")); } catch (Exception ex) { _logger.LogError("Couldn't sent mail. Error : " + ex.ToString()); _logger.LogWarning("Removing user " + user.Email + " from role " + Role.WebUser.ToString()); var resRemoveFromRole = await _userManager.RemoveFromRoleAsync(user, Role.WebUser.ToString()); if (!resRemoveFromRole.Succeeded) { _logger.LogError("Couldn't remove user " + user.Email + " from role " + Role.WebUser.ToString() + ", Error(s) : " + resRemoveFromRole.Errors.ToJsonString()); return(new KeyValuePair <int, string>(-6, "User successfully created but failed to sent email, tried to remove user but error occured.")); } _logger.LogWarning("Deleting user : "******"Couldn't delete user " + user.Email + ", Error(s) : " + resRemoveFromRole.Errors.ToJsonString()); return(new KeyValuePair <int, string>(-6, "User successfully created but failed to sent email, tried to remove user but error occured.")); } return(new KeyValuePair <int, string>(-4, "User successfully created but failed to sent email, deleted user.")); } }