public async Task Handle(EventReceived <AccountCreated> notification, CancellationToken cancellationToken) { NewAccountCreatedEmailTemplate newAccountCreatedEmailTemplate = new NewAccountCreatedEmailTemplate(notification.Event); // TODO: This is a test email await _sendGridService.SendEmailAsync( email : "*****@*****.**", subject : "ES CRM - New account created", htmlMessage : newAccountCreatedEmailTemplate.TransformText() ); }
public async Task <IActionResult> OnPostChangeEmailAsync() { var currentUser = await _userManager.GetUserAsync(User); if (currentUser == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } if (!ModelState.IsValid) { await LoadAsync(currentUser); return(Page()); } var email = await _userManager.GetEmailAsync(currentUser); if (Input.NewEmail != email) { if (await IsEmailUsedByAnotherUserAsync(Input.NewEmail, currentUser)) { StatusMessage = "Email is used by another user."; return(RedirectToPage()); } var userId = await _userManager.GetUserIdAsync(currentUser); var code = await _userManager.GenerateChangeEmailTokenAsync(currentUser, Input.NewEmail); var callbackUrl = Url.Page( "/Account/ConfirmEmailChange", pageHandler: null, values: new { userId = userId, email = Input.NewEmail, code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)) }, protocol: Request.Scheme); var message = $"We are sending you a account confirmation link.<br/>" + $"<a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>Click here to confirm</a><br/>"; await _sendGridService.SendEmailAsync(Input.NewEmail, "Email change", message); StatusMessage = "Confirmation link for email changing was sent. Please check your email."; return(RedirectToPage()); } StatusMessage = "Your email is unchanged."; return(RedirectToPage()); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } var user = await _userManager.FindByEmailAsync(Input.Email); if (user == null) { ModelState.AddModelError(string.Empty, "Verification email sent. Please check your email."); return(Page()); } var userId = await _userManager.GetUserIdAsync(user); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = userId, code = code }, protocol: Request.Scheme); var message = $"We are sending you an email confirmation link.<br/>" + $"<a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>Click here to confirm</a><br/>"; await _sendGridService.SendEmailAsync(Input.Email, "Confirmation link", message); ModelState.AddModelError(string.Empty, "Verification email sent. Please check your email."); return(Page()); }
public async Task <IActionResult> SendEmailErrorNotification(string email) { if (string.IsNullOrWhiteSpace(email)) { return(BadRequest("No email entered")); } var portal = new Portal { Id = Guid.NewGuid(), Name = "Test portal", Type = PortalType.WebApp, URL = "http://test.portal", Status = PortalStatus.Active, Email = email, CheckInterval = 1000, Method = RequestMethod.GET, BasicAuth = false }; var portalResponse = new PortalResponse { Id = Guid.NewGuid(), RequestDateTime = DateTime.Now, Status = 404, ResponseTime = 212, StatusPageId = portal.Id, ErrorMessage = "StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers: { Pragma: no-cache Vary: Accept-Encoding Date: Sun, 08 Mar 2020 02:09:59 GMT Content-Security-Policy: upgrade-insecure-requests Strict-Transport-Security: max-age=63072000; includeSubDomains; preload Cache-Control: max-age=10 Age: 0 Server: DWS Connection: keep-alive Transfer-Encoding: chunked Content-Type: text/html; charset=utf-8 }" }; var response = await _sendGridService.SendEmailAsync(portal, portalResponse); return(Ok(response)); }
public async Task <IActionResult> OnPostAsync() { if (ModelState.IsValid) { var user = await _userManager.FindByEmailAsync(Input.Email); if (user == null || !(await _userManager.IsEmailConfirmedAsync(user))) { // Don't reveal that the user does not exist or is not confirmed return(RedirectToPage("./ForgotPasswordConfirmation")); } // For more information on how to enable account confirmation and password reset please // visit https://go.microsoft.com/fwlink/?LinkID=532713 var code = await _userManager.GeneratePasswordResetTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ResetPassword", pageHandler: null, values: new { area = "Identity", code }, protocol: Request.Scheme); var message = $"We are sending you a password reset link.<br/>" + $"<a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>Click here to reset your password</a><br/>"; await _sendGridService.SendEmailAsync(Input.Email, "Password reset link", message); return(RedirectToPage("./ForgotPasswordConfirmation")); } return(Page()); }
/// <summary> /// Send notification email logic /// </summary> /// <param name="response">Response</param> /// <param name="portal">Portal responded</param> /// <returns>SendGrid>response code if email notification needed, 204 (NoContent) - if not</returns> public async Task <HttpStatusCode> SendNotificationEmailAsync(Portal portal, PortalResponse response) { // Email Notification setting incorrect, no email notification will be send if (_hoursToIgnoreContinuousError < 0) { return(HttpStatusCode.NoContent); } // Email nofitications on every error response needed var allErrorsNotification = _hoursToIgnoreContinuousError == 0; // It is the first portal error response - notification needed var firstPortalErrorNotification = portal.PortalResponses == null || portal.PortalResponses.Count <= 1 || portal.LastNotificationSent == new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified); // First error response after good one var firstErrorResponseAfterGoodOne = portal.LastRequestStatus >= 200 && portal.LastRequestStatus < 300; // Last portal error response notified is older then setted var lastErrorOlderThenSettedNotification = portal.LastNotificationSent.AddHours(_hoursToIgnoreContinuousError) < response.RequestDateTime; if (allErrorsNotification || firstPortalErrorNotification || firstErrorResponseAfterGoodOne || lastErrorOlderThenSettedNotification) { await _portalService.SetLastNotificationSentAsync(portal.Id, response.RequestDateTime); var emailProviderResponse = await _sendGridService.SendEmailAsync(portal, response); if (emailProviderResponse == HttpStatusCode.Accepted) { _logger.LogInformation($"Email for portal (Id: {portal.Id}) and response (Id: {response.Id}) quened to be send."); } else { _logger.LogWarning($"Email for portal (Id: {portal.Id}) and response (Id: {response.Id}) was not sent."); } var message = ""; if (lastErrorOlderThenSettedNotification) { message = "Last portal error response notified is older then setted"; } if (firstErrorResponseAfterGoodOne) { message = "First error response after good one"; } if (firstPortalErrorNotification) { message = "First portal error response"; } if (allErrorsNotification) { message = "Email nofitications on every error response needed"; } await _portalService.AddNotificationToHistoryAsync(portal, response.Id, response.RequestDateTime, message, (int)emailProviderResponse); return(emailProviderResponse); } return(HttpStatusCode.NoContent); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email, CreatedDateTime = DateTime.UtcNow, IsBanned = false }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { await _userManager.AddToRoleAsync(user, AppUserRoles.User); _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = user.Id, code = code }, protocol: Request.Scheme); var message = $"We are sending you an email confirmation link.<br/>" + $"<a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>Click here to confirm</a><br/>"; await _sendGridService.SendEmailAsync(Input.Email, "Confirmation link", message); if (_userManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToPage("RegisterConfirmation", new { receiverEmail = Input.Email })); } else { await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task SendEmailAsync_WhenInputIsInvalid_ShouldReturnNull() { var result = await _sendGridService.SendEmailAsync("", null, "message"); Assert.IsNull(result); }