Marius Vasile

Marius Vasile

  • 594
  • 1.9k
  • 145.6k

asp.net core user registration with email confirmation

Feb 21 2021 4:13 PM
I am trying to add email confirmation to user registration. I 've done the following so far
 
Added to Register.cshtml.cs 
  1. var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);  
  2.                         code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));  
  3.   
  4.                         var callbackUrl = Url.Page("/Account/ConfirmEmail",  
  5.                             pageHandler: null,  
  6.                             values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },  
  7.                             protocol: Request.Scheme);  
  8.   
  9.                         await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",  
  10.                             $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");  
 Create a Services folder and class EmailServices.cs
 
  1. using MailKit.Net.Smtp;  
  2. using Microsoft.AspNetCore.Hosting;  
  3. using Microsoft.Extensions.Hosting;  
  4. using Microsoft.Extensions.Options;  
  5. using MimeKit;  
  6. using System;  
  7. using System.Threading.Tasks;  
  8. using RoSafety.Services;  
  9.   
  10. namespace RoSafety.Services  
  11. {  
  12.     public interface IMailer  
  13.     {  
  14.         Task SendEmailAsync(string email, string subject, string body);  
  15.     }  
  16.   
  17.     public class Mailer : IMailer  
  18.     {  
  19.         private readonly SmtpSettings _smtpSettings;  
  20.         private readonly IWebHostEnvironment _env;  
  21.   
  22.         public Mailer(IOptions<SmtpSettings> smtpSettings, IWebHostEnvironment env)  
  23.         {  
  24.             _smtpSettings = smtpSettings.Value;  
  25.             _env = env;  
  26.         }  
  27.   
  28.         public async Task SendEmailAsync(string email, string subject, string body)  
  29.         {  
  30.             try  
  31.             {  
  32.                 var message = new MimeMessage();  
  33.                 message.From.Add(new MailboxAddress(_smtpSettings.SenderName, _smtpSettings.SenderEmail));  
  34.                 message.To.Add(new MailboxAddress(email));  
  35.                 message.Subject = subject;  
  36.                 message.Body = new TextPart("html")  
  37.                 {  
  38.                     Text = body  
  39.                 };  
  40.   
  41.                 using (var client = new SmtpClient())  
  42.                 {  
  43.                     client.ServerCertificateValidationCallback = (s, c, h, e) => true;  
  44.   
  45.                     if (_env.IsDevelopment())  
  46.                     {  
  47.                         await client.ConnectAsync(_smtpSettings.Server, _smtpSettings.Port, true);  
  48.                     }  
  49.                     else  
  50.                     {  
  51.                         await client.ConnectAsync(_smtpSettings.Server);  
  52.                     }  
  53.   
  54.                     await client.AuthenticateAsync(_smtpSettings.UserName, _smtpSettings.Password);  
  55.                     await client.SendAsync(message);  
  56.                     await client.DisconnectAsync(true);  
  57.                 }  
  58.             }  
  59.             catch (Exception e)  
  60.             {  
  61.                 throw new InvalidOperationException(e.Message);  
  62.             }  
  63.         }  
  64.     }  
  65.     public class SmtpSettings  
  66.     {  
  67.         public string Server { get; set; }  
  68.         public int Port { get; set; }  
  69.         public string SenderName { get; set; }  
  70.         public string SenderEmail { get; set; }  
  71.         public string UserName { get; set; }  
  72.         public string Password { get; set; }  
  73.     }  
  74. }  
added smtp settings to json
 
  1. "SmtpSettings": {  
  2.     "Server""smtp.gmail.com",  
  3.     "Port": 587,  
  4.     "SenderName""admin",  
  5.     "SenderEmail""[email protected]",  
  6.     "UserName""[email protected]",  
  7.     "Password""password"  
  8.   },  
and startup
 
  1. services.AddIdentity<UserDataClass, UserRoleClass>(options =>  
  2.             {  
  3.                 options.SignIn.RequireConfirmedAccount = true;  
  4.                 options.User.RequireUniqueEmail = true;  
  5.                 options.Password.RequiredLength = 8;  
  6.                 options.Password.RequireDigit = true;  
  7.                 options.Password.RequireUppercase = true;  
  8.   
  9.             })  
  10.                 .AddDefaultUI()  
  11.                 .AddRoles<UserRoleClass>()  
  12.                 .AddDefaultTokenProviders()  
  13.                 .AddEntityFrameworkStores<ApplicationDbContext>();  
  14.   
  15.   
  16.             services.Configure<SmtpSettings>(Configuration.GetSection("SmtpSettings"));  
  17.             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? 

Answers (6)