public async Task <IActionResult> ResetPassword(string email) // This endpoint may not be used in production, instead sending email to admin for resetting password { var user = await _userManager.FindByEmailAsync(email); var restToken = await _userManager.GeneratePasswordResetTokenAsync(user); var resetLink = Url.Action("ResetPassword", "Account", new { token = restToken }, protocol: HttpContext.Request.Scheme); // Send email - Send message to message queue(notificaiton) - integration event var emailBody = ""; EmailNotificationEvent e = new EmailNotificationEvent(new Guid(), email, "Password Reset", emailBody); try { await _messagePublisher.PublishMessageAsync(e.MessageType, e, "notification"); Log.Information("Message {MessageType} with Id {MessageId} has been published successfully", e.MessageType, e.MessageId); } catch (Exception ex) { //throw ex; Log.Error(ex, "Error while publishing {MessageType} message with id {MessageId}.", e.MessageType, e.MessageId); } return(Ok(resetLink)); }
/// <summary> /// 发送激活邮件 /// </summary> /// <param name="email"></param> private void SendActivateMail(UserDTO user) { //string template = $""; EmailNotificationEvent emailNotificationEvent = new EmailNotificationEvent() { Email = user.Email }; }
private async Task HandleAsync(EmailNotificationEvent @event) { try { await _smtpMailSender.SendEmailAsync(@event.Email, "", @event.Subject, @event.Body); //await _emailSender.SendEmailAsync(@event.NotificationRecipients, @event.NotificationSubject, @event.NotificationBody); } catch (Exception ex) { //throw ex; Log.Error(ex, "Error while sending notification message to { user }.", @event.Email); } //throw new NotImplementedException(); }
public async Task <string> Handle(RegisterCommand request, CancellationToken cancellationToken) { //if (!ModelState.IsValid) //{ // return BadRequest(ModelState); //} var configSection = _config.GetSection("ServiceUri"); string AssetServiceHost = configSection["Asset"]; var user = new ApplicationUser { UserName = request.UserName, Email = request.Email, // it is critical here, it comes from client via query string AvatarImgUrl = "Images/Avatars/default.png", //FirstName = request.FirstName, //LastName = request.LastName, JoinDate = DateTime.Now, LastUpdated = DateTime.Now, EmailConfirmed = true, UserRole = request.UserRole, CustomId = 0, IsDisabled = false, // The following are not required here Telephone1 = request.Telephone1, Telephone2 = request.Telephone2, AddressStreet = request.AddressStreet, AddressCity = request.AddressCity, AddressStateProv = request.AddressProvState, AddressZipPostCode = request.AddressPostZipCode, AddressCountry = request.AddressCountry }; try { if (user.UserRole != "pm") // Verify the eligibility of registration for users other than property manager (pm) { // Verify the eligibility of registration *********************************** HttpClient client = new HttpClient(); client.BaseAddress = new Uri(AssetServiceHost + "api/Property"); var status = await client.GetAsync(client.BaseAddress + "/user/" + user.Email); status.EnsureSuccessStatusCode(); string responseBody = await status.Content.ReadAsStringAsync(); //.;CopyToAsync() if (responseBody == "false") { return("Not eligible for self-registration"); } // New code ******************************************************************* var queryString = AssetServiceHost + "api/Property/userInfo/" + request.Email; var response = await client.GetAsync(queryString); var content = response.Content.ReadAsStringAsync(); JObject json = JObject.Parse(content.Result); bool online = json.SelectToken("onlineEnabled").Value <bool>(); if (!online) { return("Not eligible for self-registration"); } //user.Email = json.SelectToken("email").Value<string>(); // it takes from input request user.FirstName = json.SelectToken("firstName").Value <string>(); user.LastName = json.SelectToken("lastName").Value <string>(); user.Telephone1 = json.SelectToken("telephone1").Value <string>(); user.Telephone2 = json.SelectToken("telephone2").Value <string>(); user.LastName = json.SelectToken("lastName").Value <string>(); user.SocialMediaContact1 = json.SelectToken("socialMediaContact1").Value <string>(); user.SocialMediaContact2 = json.SelectToken("socialMediaContact2").Value <string>(); user.AddressStreet = json.SelectToken("addressStreet").Value <string>(); user.AddressCity = json.SelectToken("addressCity").Value <string>(); user.AddressStateProv = json.SelectToken("addressProvState").Value <string>(); user.AddressZipPostCode = json.SelectToken("addressPostZipCode").Value <string>(); user.AddressCountry = json.SelectToken("addressCountry").Value <string>(); } // Create the account var result = await _userManager.CreateAsync(user, request.Password); // Add role to the new user var role_resuls = await _userManager.AddToRoleAsync(user, request.UserRole); if (!result.Succeeded /*&& !role_resuls.Succeeded*/) { return("Registration failed"); //new BadRequestObjectResult(Errors.AddErrorsToModelState(result, ModelState)); } //await _appDbContext.Customers.AddAsync(new Customer { IdentityId = userIdentity.Id, Location = model.Location }); //await _appDbContext.SaveChangesAsync(); // commented out for testing ONLY // Raise domain event for email notificaiton or directly invoke email sending string subject = "Account Registration"; // Imporve the follwing with string buidler in C# string body = "Dear " + user.FirstName + " " + user.LastName + ":" + "\n" + "Your account registration has completed successfully, you can login now."; /* * AccountRegistrationEvent e = new AccountRegistrationEvent(user.Email, user.UserName, "", subject, body); * * await _mediator.Publish(e); * */ //Send message to message queue(notificaiton) - integratin event EmailNotificationEvent e = new EmailNotificationEvent(Guid.NewGuid(), user.Email, body, subject); // Send message to message queue for other service //RegisterAccountEvent e = new RegisterAccountEvent(Guid.NewGuid(), user.Email, "*****@*****.**", user.UserName, body, subject); // This event will be re-defined // Send message to message queue for other services, e.g.lease service if the roleId is other type, e.g. tenant, vendor, etc. try { await _messagePublisher.PublishMessageAsync(e.MessageType, e, "notification"); Log.Information("Message {MessageType} with Id {MessageId} has been published successfully", e.MessageType, e.MessageId); } catch (Exception ex) { //throw ex; Log.Error(ex, "Error while publishing {MessageType} message with id {MessageId}.", e.MessageType, e.MessageId); } } catch (Exception ex) { //throw ex; return("Error occured: " + ex.Message); } return("User Account: " + user.UserName + " Successfully Created!"); //throw new NotImplementedException(); }