Skip to content

Commit 4503573

Browse files
Send email for the email verification
1 parent 09a9bd3 commit 4503573

File tree

6 files changed

+65
-3
lines changed

6 files changed

+65
-3
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title></title>
6+
</head>
7+
<body>
8+
<p>Hello {{UserName}}, <br />
9+
Thank you for using the Book store web application. <br />
10+
You have created a new account on book store application. <br /> <br />
11+
Click on the following link to verify your email id. <br /><br />
12+
13+
<a href="{{Link}}">Verify email</a>
14+
</body>
15+
</html>

Webgentle.BookStore/Webgentle.BookStore/Repository/AccountRepository.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Identity;
2+
using Microsoft.Extensions.Configuration;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -13,14 +14,20 @@ public class AccountRepository : IAccountRepository
1314
private readonly UserManager<ApplicationUser> _userManager;
1415
private readonly SignInManager<ApplicationUser> _signInManager;
1516
private readonly IUserService _userService;
17+
private readonly IEmailService _emailService;
18+
private readonly IConfiguration _configuration;
1619

1720
public AccountRepository(UserManager<ApplicationUser> userManager,
1821
SignInManager<ApplicationUser> signInManager,
19-
IUserService userService)
22+
IUserService userService,
23+
IEmailService emailService,
24+
IConfiguration configuration)
2025
{
2126
_userManager = userManager;
2227
_signInManager = signInManager;
2328
_userService = userService;
29+
_emailService = emailService;
30+
_configuration = configuration;
2431
}
2532

2633
public async Task<IdentityResult> CreateUserAsync(SignUpUserModel userModel)
@@ -34,6 +41,14 @@ public async Task<IdentityResult> CreateUserAsync(SignUpUserModel userModel)
3441

3542
};
3643
var result = await _userManager.CreateAsync(user, userModel.Password);
44+
if (result.Succeeded)
45+
{
46+
var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
47+
if (!string.IsNullOrEmpty(token))
48+
{
49+
await SendEmailConfirmationEmail(user, token);
50+
}
51+
}
3752
return result;
3853
}
3954

@@ -53,5 +68,24 @@ public async Task<IdentityResult> ChangePasswordAsync(ChangePasswordModel model)
5368
var user = await _userManager.FindByIdAsync(userId);
5469
return await _userManager.ChangePasswordAsync(user, model.CurrentPassword, model.NewPassword);
5570
}
71+
72+
private async Task SendEmailConfirmationEmail(ApplicationUser user, string token)
73+
{
74+
string appDomain = _configuration.GetSection("Application:AppDomain").Value;
75+
string confirmationLink = _configuration.GetSection("Application:EmailConfirmation").Value;
76+
77+
UserEmailOptions options = new UserEmailOptions
78+
{
79+
ToEmails = new List<string>() { user.Email },
80+
PlaceHolders = new List<KeyValuePair<string, string>>()
81+
{
82+
new KeyValuePair<string, string>("{{UserName}}", user.FirstName),
83+
new KeyValuePair<string, string>("{{Link}}",
84+
string.Format(appDomain + confirmationLink, user.Id, token))
85+
}
86+
};
87+
88+
await _emailService.SendEmailForEmailConfirmation(options);
89+
}
5690
}
5791
}

Webgentle.BookStore/Webgentle.BookStore/Service/EmailService.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ public async Task SendTestEmail(UserEmailOptions userEmailOptions)
2525
await SendEmail(userEmailOptions);
2626
}
2727

28+
public async Task SendEmailForEmailConfirmation(UserEmailOptions userEmailOptions)
29+
{
30+
userEmailOptions.Subject = UpdatePlaceHolders("Hello {{UserName}}, Confirm your email id.", userEmailOptions.PlaceHolders);
31+
32+
userEmailOptions.Body = UpdatePlaceHolders(GetEmailBody("EmailConfirm"), userEmailOptions.PlaceHolders);
33+
34+
await SendEmail(userEmailOptions);
35+
}
36+
2837
public EmailService(IOptions<SMTPConfigModel> smtpConfig)
2938
{
3039
_smtpConfig = smtpConfig.Value;

Webgentle.BookStore/Webgentle.BookStore/Service/IEmailService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ namespace Webgentle.BookStore.Service
66
public interface IEmailService
77
{
88
Task SendTestEmail(UserEmailOptions userEmailOptions);
9+
10+
Task SendEmailForEmailConfirmation(UserEmailOptions userEmailOptions);
911
}
1012
}

Webgentle.BookStore/Webgentle.BookStore/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void ConfigureServices(IServiceCollection services)
3737
options => options.UseSqlServer(_configuration.GetConnectionString("DefaultConnection")));
3838

3939
services.AddIdentity<ApplicationUser, IdentityRole>()
40-
.AddEntityFrameworkStores<BookStoreContext>();
40+
.AddEntityFrameworkStores<BookStoreContext>().AddDefaultTokenProviders();
4141

4242
services.Configure<IdentityOptions>(options =>
4343
{

Webgentle.BookStore/Webgentle.BookStore/appsettings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"AllowedHosts": "*",
1313
"AppName": "Book store App",
1414
"Application": {
15-
"LoginPath": "/login"
15+
"LoginPath": "/login",
16+
"AppDomain": "http://localhost:65352/",
17+
"EmailConfirmation": "confirm-email?uid={0}&token={1}"
1618
},
1719

1820
"NewBookAlert": {

0 commit comments

Comments
 (0)