The main objectives of this article are:
(Step 2) Now check your registered email and you can see our application email confirmation email below.
- Use SendGrid Email Service.
- Implement Email Confirmation For User Registration
Send Grid:
SendGrid is a third-party email provider from which we deal with 'send' and 'receive' operations of email to our application.
SendGrid SMTP(Simple Mail Transfer Protocol) provider for email transfer. SendGrid also provides SendGrid .Net NuGet package that provides easy configuration to any .NET application. So to use the SendGrid SMTP we need 'Auth Key', 'From Email'. So let's register into SendGrid.
SendGrid SMTP(Simple Mail Transfer Protocol) provider for email transfer. SendGrid also provides SendGrid .Net NuGet package that provides easy configuration to any .NET application. So to use the SendGrid SMTP we need 'Auth Key', 'From Email'. So let's register into SendGrid.
The following are the steps to register and generate the API key in SendGrid:
(Step 1)
Go to SendGrid's official website at 'https://sendgrid.com/'. Next do SingUp.
(Step 2)
Now login in to the SendGrid website, then under the left-hand side menu go to the 'Settings' ==> 'Sender Authentication'. At 'Single Sender Verification' click on the 'Get Started' button.
(Step 3)
Let's register the 'From Email Address', so let's complete the 'Create A Sender' form.
(Step 4)
Our 'From Email Address' needs to be verified by clicking on the email we received from the SendGrid.
(Step 5)
Left-hand side menu 'Settings' ==> 'API Keys'. Then Click on the 'Create API Key' button.
(Step 6)
Choose the permission level for the API key and then generate.
(Step 7)
API key is generated, copy it, and save it because it will display only once.
Configure SendGrid Settings:
Let's configure the SendGrid setting in our 'appsetting.Development.json' file.
appsettings.Development.json:
"SendGridSettings": { "ApiKey": "", "FromEmail": "", "EmailName": "" }Now create a class like 'SendGridSettings' in the 'Settings' folder(new folder).
Settings/SendGridSettings.cs:
namespace RazorPage.AspNetIdentity.Settings; public class SendGridSettings { public string ApiKey { get; set; } public string FromEmail { get; set; } public string EmailName { get; set; } }Now map our 'SendGridSettings' entity with our configuration in 'appsettings.Development.json'.
Program.cs:
builder.Services.Configure<SendGridSettings>(builder.Configuration.GetSection("SendGridSettings"));
Install SendGrid NuGet Packages:
Let's install the SendGrid required package into our application.
Visual Studio 2022:
Install-Package SendGrid -Version 9.28.1
Install-Package SendGrid.Extensions.DependencyInjection -Version 1.0.1
Visual Studio Code:
dotnet add package SendGrid --version 9.28.1
dotnet add package SendGrid.Extensions.DependencyInjection --version 1.0.1
Register SendGrid Service:
Let's register our SendGrid service in 'Program.cs'
Program.cs:
builder.Services.AddSendGrid(options => { options.ApiKey = builder.Configuration.GetSection("SendGridSettings") .GetValue<string>("ApiKey"); });
- Here configured the API key
Implement Asp.Net Core Identity 'IEmailSender':
Let's create a class like 'EmailSenderService' in the 'Services' folder(new folder) that will implement 'Microsoft.AspNetCore.Identity.UI.Services.IEmailSender'.
Services/EmailSenderService.cs:
using Microsoft.AspNetCore.Identity.UI.Services; using Microsoft.Extensions.Options; using RazorPage.AspNetIdentity.Settings; using SendGrid; using SendGrid.Helpers.Mail; namespace RazorPage.AspNetIdentity.Services; public class EmailSenderService : IEmailSender { private readonly ISendGridClient _sendGridClient; private readonly SendGridSettings _sendGridSettings; public EmailSenderService(ISendGridClient sendGridClient, IOptions<SendGridSettings> sendGridSettings) { _sendGridClient = sendGridClient; _sendGridSettings = sendGridSettings.Value; } public async Task SendEmailAsync(string email, string subject, string htmlMessage) { var msg = new SendGridMessage() { From = new EmailAddress(_sendGridSettings.FromEmail, _sendGridSettings.EmailName), Subject = subject, HtmlContent = htmlMessage }; msg.AddTo(email); await _sendGridClient.SendEmailAsync(msg); } }
- (Line: 9) The 'EmailSenderService' inherits 'Microsoft.AspNetCore.Identity.UI.Services.IEmailSender'.
- (Line: 11- 18) Injected the 'SendGridSettings' and 'ISendGridClient'
- (Line: 23) Configuring our send grid 'From EmailAddress'.
- (Line: 24) Email Subject
- (Line: 25) HTML email body.
- (Line: 27) Configuring the 'To Email Address'
Program.cs:
builder.Services.AddScoped<IEmailSender,EmailSenderService>();
Remove Fake Email Confirmation Code:
Now go to 'Areas/Identity/Pages/Account/RegistrationConfirmation.cshtml.cs' file and comment on the below-shown code inside of the 'OnGetAsync' method.
Areas/Identity/Pages/Account/RegistrationConfirmation.cshtml.cs:
(Step 1) Go to the registration page and get registered you can see the message below.
In the next article, we are going to implement Two-Factor authentication using Email.
Support Me!
Buy Me A Coffee
PayPal Me
Video Session:
Wrapping Up:
Hopefully, I think this article delivered some useful information on the Asp.Net Core Identity In Razor Pages. using I love to have your feedback, suggestions, and better techniques in the comment section below
Refer:
Part -1 | Asp.Net Core Identity Series[.NET 7] Introduction & Project Setup
Part-3 | Asp.Net Core Identity Series[.NET 7] | Sending Two-Factor Authentication(2FA) Code To Email
Part-3 | Asp.Net Core Identity Series[.NET 7] | Sending Two-Factor Authentication(2FA) Code To Email
Part-4 | Asp.Net Core Identity Series[.NET 7] | Sending Two-Factor Authentication(2FA) Code To Phone
Part - 6 | Asp.Net Core Identity Series[.NET 7] | Facebook Authentication
Part - 7 | Asp.Net Core Identity Series[.NET 7] | Twitter Authentication
Part - 7 | Asp.Net Core Identity Series[.NET 7] | Twitter Authentication
Comments
Post a Comment