I am trying to add email confirmation to user registration. I 've done the following so far
Added to Register.cshtml.cs
- 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, returnUrl = returnUrl },
- protocol: Request.Scheme);
- await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
- $"Please confirm your account by clicking here.");
- using MailKit.Net.Smtp;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.Extensions.Hosting;
- using Microsoft.Extensions.Options;
- using MimeKit;
- using System;
- using System.Threading.Tasks;
- using RoSafety.Services;
- namespace RoSafety.Services
- {
- public interface IMailer
- {
- Task SendEmailAsync(string email, string subject, string body);
- }
- public class Mailer : IMailer
- {
- private readonly SmtpSettings _smtpSettings;
- private readonly IWebHostEnvironment _env;
- public Mailer(IOptions
smtpSettings, IWebHostEnvironment env) - {
- _smtpSettings = smtpSettings.Value;
- _env = env;
- }
- public async Task SendEmailAsync(string email, string subject, string body)
- {
- try
- {
- var message = new MimeMessage();
- message.From.Add(new MailboxAddress(_smtpSettings.SenderName, _smtpSettings.SenderEmail));
- message.To.Add(new MailboxAddress(email));
- message.Subject = subject;
- message.Body = new TextPart("html")
- {
- Text = body
- };
- using (var client = new SmtpClient())
- {
- client.ServerCertificateValidationCallback = (s, c, h, e) => true;
- if (_env.IsDevelopment())
- {
- await client.ConnectAsync(_smtpSettings.Server, _smtpSettings.Port, true);
- }
- else
- {
- await client.ConnectAsync(_smtpSettings.Server);
- }
- await client.AuthenticateAsync(_smtpSettings.UserName, _smtpSettings.Password);
- await client.SendAsync(message);
- await client.DisconnectAsync(true);
- }
- }
- catch (Exception e)
- {
- throw new InvalidOperationException(e.Message);
- }
- }
- }
- public class SmtpSettings
- {
- public string Server { get; set; }
- public int Port { get; set; }
- public string SenderName { get; set; }
- public string SenderEmail { get; set; }
- public string UserName { get; set; }
- public string Password { get; set; }
- }
- }
- "SmtpSettings": {
- "Server": "smtp.gmail.com",
- "Port": 587,
- "SenderName": "admin",
- "SenderEmail": "[email protected]",
- "UserName": "[email protected]",
- "Password": "password"
- },
- services.AddIdentity
(options => - {
- options.SignIn.RequireConfirmedAccount = true;
- options.User.RequireUniqueEmail = true;
- options.Password.RequiredLength = 8;
- options.Password.RequireDigit = true;
- options.Password.RequireUppercase = true;
- })
- .AddDefaultUI()
- .AddRoles
() - .AddDefaultTokenProviders()
- .AddEntityFrameworkStores
(); - services.Configure
(Configuration.GetSection("SmtpSettings")); - services.AddSingleton
();

Marius VasilePosted Feb 22, 2021, 4:52 PM
Marius VasilePosted Feb 22, 2021, 4:14 PM
Sachin SinghPosted Feb 22, 2021, 4:04 PM
Marius VasilePosted Feb 22, 2021, 1:35 PM
Sachin SinghPosted Feb 21, 2021, 4:50 PM
Marius VasilePosted Feb 21, 2021, 4:25 PM