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 <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
Create a Services folder and class EmailServices.cs
- 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> 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; }
- }
- }
added smtp settings to json
- "SmtpSettings": {
- "Server": "smtp.gmail.com",
- "Port": 587,
- "SenderName": "admin",
- "SenderEmail": "[email protected]",
- "UserName": "[email protected]",
- "Password": "password"
- },
and startup
- services.AddIdentity<UserDataClass, UserRoleClass>(options =>
- {
- options.SignIn.RequireConfirmedAccount = true;
- options.User.RequireUniqueEmail = true;
- options.Password.RequiredLength = 8;
- options.Password.RequireDigit = true;
- options.Password.RequireUppercase = true;
-
- })
- .AddDefaultUI()
- .AddRoles<UserRoleClass>()
- .AddDefaultTokenProviders()
- .AddEntityFrameworkStores<ApplicationDbContext>();
-
-
- services.Configure<SmtpSettings>(Configuration.GetSection("SmtpSettings"));
- services.AddSingleton<IMailer, Mailer>();
but a new account is still automatically confirmed and I don't receive email for confirmation. Can anybody see what is wrong please?