private async Task SendForgotPasswordEmail(WebProfileUser user, string email) { var code = await _userManager.GeneratePasswordResetTokenAsync(user); var callbackUrl = Url.Page( "/Account/ResetPassword", null, new { code, email }, Request.Scheme); var message = System.IO.File.ReadAllText(Path.Combine(_host.WebRootPath, "emailtemplates/ForgotPassword.html")); message = message.Replace("%NAME%", user.Nome); message = message.Replace("%SITE%", "https://relprev.dvt.infraero.gov.br"); message = message.Replace("%TEMPOTOKEN%", _parameters.Value.TokenTime.ToString()); message = message.Replace("%HORA%", DateTime.Now.ToString("hh:mm")); message = message.Replace("%DATA%", DateTime.Now.ToString("dd/MM/yyyy")); message = message.Replace("%CALLBACK%", HtmlEncoder.Default.Encode(callbackUrl)); await _emailSender.SendEmailAsync(user.Email, "Recupere sua senha do sistema Relprev", message); }
public async Task <ActionResult <long> > CreateUsuario(CreateUsuarioCommand command) { try { var result = new IdentityResult(); var user = new WebProfileUser { DateRegistered = DateTime.UtcNow, UserName = command.UserName, Email = command.Email, Nome = command.Nome, Cpf = command.Cpf, Telefone = command.Telefone }; var existingUser = await _userManager.FindByEmailAsync(user.UserName); if (existingUser != null) { ModelState.AddModelError(string.Empty, "Usuario informado já está cadastrado."); } // Create the user in the database result = await _userManager.CreateAsync(user, "12345678"); var userRole = _db.Roles.FirstOrDefault(x => x.Id == command.CodPerfil).Name; long resultUsu = 0; if (result.Succeeded) { await _userManager.AddToRoleAsync(user, userRole); command.Id = user.Id; command.NomUsuario = user.Nome; command.NumCpf = user.Cpf; command.NumTelefone = user.Telefone; command.DthRegistro = user.DateRegistered.ToString(); command.Email = user.Email; command.NomPerfil = userRole; resultUsu = await Mediator.Send(command); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } return(resultUsu); } catch (Exception e) { Console.WriteLine(e); throw; } }
private async Task SendNewUserEmail(WebProfileUser user, string email) { var code = await _userManager.GeneratePasswordResetTokenAsync(user); var callbackUrl = Url.ActionLink("ResetPassword", "Identity/Account", new { code, email }); var message = System.IO.File.ReadAllText(Path.Combine(_host.WebRootPath, "emailtemplates/ConfirmEmail.html")); message = message.Replace("%NAME%", user.Nome); message = message.Replace("%CALLBACK%", HtmlEncoder.Default.Encode(callbackUrl.Replace("%2FAccount", "/Account"))); await _emailSender.SendEmailAsync(user.Email, "Primeiro acesso sistema Relprev", message); }
private async Task SendForgotPasswordEmail(WebProfileUser user) { var code = await _userManager.GeneratePasswordResetTokenAsync(user); var callbackUrl = Url.Page( "/Account/ResetPassword", null, new { code }, Request.Scheme); var message = System.IO.File.ReadAllText(Path.Combine(_host.WebRootPath, "emailtemplates/ForgotPassword.html")); message = message.Replace("%NAME%", user.FirstName); message = message.Replace("%CALLBACK%", HtmlEncoder.Default.Encode(callbackUrl)); await _emailSender.SendEmailAsync(user.Email, "Reset Your Admin Template Password", message); }
private async Task SendEmailConfirmation(WebProfileUser user) { var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", null, new { userId = user.Id, code }, Request.Scheme); var message = System.IO.File.ReadAllText(Path.Combine(_host.WebRootPath, "emailtemplates/ConfirmEmail.html")); message = message.Replace("%NAME%", Register.FirstName); message = message.Replace("%CALLBACK%", HtmlEncoder.Default.Encode(callbackUrl)); await _emailSender.SendEmailAsync(Register.Email, "Confirm Your Admin Portal Registration", message); ConfirmEmailSent = true; }
public async Task <IActionResult> OnPostRegisterAsync(string returnUrl = null) { ConfirmEmailSent = false; if (ModelState.IsValid) { var user = new WebProfileUser { DateRegistered = DateTime.UtcNow, UserName = Register.Email, Email = Register.Email, FirstName = Register.FirstName, LastName = Register.LastName, Company = Register.Company }; // If there is already a user in the system with this email, and they have not confirmed thier email // send them a new confirmation email rather than trying to register them. var existingUser = await _userManager.FindByEmailAsync(user.UserName); if (existingUser != null && !existingUser.EmailConfirmed) { await SendEmailConfirmation(existingUser); return(Page()); } // Create the user in the database var result = await _userManager.CreateAsync(user, Register.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); // Setup database roles //var userCount = _db.Users.Count(); var roleCount = _db.Roles.Count(); // NOTE: This is not necessarily best practice at all. // First registered user will add the default roles, rather than doing this in a migration. // First registered user will be given all roles. if (roleCount == 0) { await _roleManager.CreateAsync(new IdentityRole { Name = UserRoles.Administrator }); await _roleManager.CreateAsync(new IdentityRole { Name = UserRoles.SuperUser }); await _roleManager.CreateAsync(new IdentityRole { Name = UserRoles.Registered }); // set this registering user as admin/everything await _userManager.AddToRolesAsync(user, new[] { UserRoles.Administrator, UserRoles.SuperUser, UserRoles.Registered }); } await _userManager.AddToRoleAsync(user, UserRoles.Registered); await SendEmailConfirmation(user); // If you want to login the user without awaiting email confirmation, uncomment this line. //await _signInManager.SignInAsync(user, isPersistent: false); return(Page()); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }