Skip to content

Commit 8a944a2

Browse files
Send email for reset password
1 parent cb699a9 commit 8a944a2

File tree

10 files changed

+143
-1
lines changed

10 files changed

+143
-1
lines changed

Webgentle.BookStore/Webgentle.BookStore/Controllers/AccountController.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
67
using Webgentle.BookStore.Models;
78
using Webgentle.BookStore.Repository;
@@ -162,5 +163,29 @@ public async Task<IActionResult> ConfirmEmail(EmailConfirmModel model)
162163
}
163164
return View(model);
164165
}
166+
167+
[AllowAnonymous, HttpGet("fotgot-password")]
168+
public IActionResult ForgotPassword()
169+
{
170+
return View();
171+
}
172+
173+
[AllowAnonymous, HttpPost("fotgot-password")]
174+
public async Task<IActionResult> ForgotPassword(ForgotPasswordModel model)
175+
{
176+
if (ModelState.IsValid)
177+
{
178+
// code here
179+
var user = await _accountRepository.GetUserByEmailAsync(model.Email);
180+
if (user != null)
181+
{
182+
await _accountRepository.GenerateForgotPasswordTokenAsync(user);
183+
}
184+
185+
ModelState.Clear();
186+
model.EmailSent = true;
187+
}
188+
return View(model);
189+
}
165190
}
166191
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title></title>
6+
</head>
7+
<body>
8+
<p>
9+
Hello {{UserName}}, <br />
10+
Click on the following link to reset your password. <br /><br />
11+
12+
<a href="{{Link}}">Verify email</a>
13+
</body>
14+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
namespace Webgentle.BookStore.Models
8+
{
9+
public class ForgotPasswordModel
10+
{
11+
[Required, EmailAddress, Display(Name ="Registered email address")]
12+
public string Email { get; set; }
13+
public bool EmailSent { get; set; }
14+
}
15+
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ public async Task GenerateEmailConfirmationTokenAsync(ApplicationUser user)
6262
}
6363
}
6464

65+
public async Task GenerateForgotPasswordTokenAsync(ApplicationUser user)
66+
{
67+
var token = await _userManager.GeneratePasswordResetTokenAsync(user);
68+
if (!string.IsNullOrEmpty(token))
69+
{
70+
await SendForgotPasswordEmail(user, token);
71+
}
72+
}
73+
6574
public async Task<SignInResult> PasswordSignInAsync(SignInModel signInModel)
6675
{
6776
return await _signInManager.PasswordSignInAsync(signInModel.Email, signInModel.Password, signInModel.RememberMe, false);
@@ -103,5 +112,24 @@ private async Task SendEmailConfirmationEmail(ApplicationUser user, string token
103112

104113
await _emailService.SendEmailForEmailConfirmation(options);
105114
}
115+
116+
private async Task SendForgotPasswordEmail(ApplicationUser user, string token)
117+
{
118+
string appDomain = _configuration.GetSection("Application:AppDomain").Value;
119+
string confirmationLink = _configuration.GetSection("Application:ForgotPassword").Value;
120+
121+
UserEmailOptions options = new UserEmailOptions
122+
{
123+
ToEmails = new List<string>() { user.Email },
124+
PlaceHolders = new List<KeyValuePair<string, string>>()
125+
{
126+
new KeyValuePair<string, string>("{{UserName}}", user.FirstName),
127+
new KeyValuePair<string, string>("{{Link}}",
128+
string.Format(appDomain + confirmationLink, user.Id, token))
129+
}
130+
};
131+
132+
await _emailService.SendEmailForForgotPassword(options);
133+
}
106134
}
107135
}

Webgentle.BookStore/Webgentle.BookStore/Repository/IAccountRepository.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ public interface IAccountRepository
1919
Task<IdentityResult> ConfirmEmailAsync(string uid, string token);
2020

2121
Task GenerateEmailConfirmationTokenAsync(ApplicationUser user);
22+
23+
Task GenerateForgotPasswordTokenAsync(ApplicationUser user);
2224
}
2325
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ public async Task SendEmailForEmailConfirmation(UserEmailOptions userEmailOption
3434
await SendEmail(userEmailOptions);
3535
}
3636

37+
public async Task SendEmailForForgotPassword(UserEmailOptions userEmailOptions)
38+
{
39+
userEmailOptions.Subject = UpdatePlaceHolders("Hello {{UserName}}, reset your password.", userEmailOptions.PlaceHolders);
40+
41+
userEmailOptions.Body = UpdatePlaceHolders(GetEmailBody("ForgotPassword"), userEmailOptions.PlaceHolders);
42+
43+
await SendEmail(userEmailOptions);
44+
}
45+
3746
public EmailService(IOptions<SMTPConfigModel> smtpConfig)
3847
{
3948
_smtpConfig = smtpConfig.Value;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ public interface IEmailService
88
Task SendTestEmail(UserEmailOptions userEmailOptions);
99

1010
Task SendEmailForEmailConfirmation(UserEmailOptions userEmailOptions);
11+
12+
Task SendEmailForForgotPassword(UserEmailOptions userEmailOptions);
1113
}
1214
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@model Webgentle.BookStore.Models.ForgotPasswordModel
2+
3+
@{
4+
ViewData["Title"] = "ForgotPassword";
5+
}
6+
7+
<div class="container">
8+
9+
<div class="row">
10+
<div class="col-md-4 offset-4">
11+
@if (Model?.EmailSent == true)
12+
{
13+
<div class="alert alert-success" role="alert">
14+
<p>If you are registered with us, <br />
15+
We have sent an email on your registered email id</p>
16+
</div>
17+
}
18+
else
19+
{
20+
<p>Forgot your password? No worries you can reset it easily.</p>
21+
<form asp-action="ForgotPassword">
22+
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
23+
<div class="form-group">
24+
<label asp-for="Email" class="control-label"></label>
25+
<input asp-for="Email" class="form-control" />
26+
<span asp-validation-for="Email" class="text-danger"></span>
27+
</div>
28+
29+
<div class="form-group">
30+
<input type="submit" value="Continue" class="btn btn-primary" />
31+
</div>
32+
</form>
33+
}
34+
<div>
35+
<p>Back to <a asp-action="Login" asp-controller="Account">Login</a></p>
36+
</div>
37+
</div>
38+
39+
</div>
40+
41+
42+
43+
44+
</div>

Webgentle.BookStore/Webgentle.BookStore/Views/Account/Login.cshtml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
</div>
6161

6262
<div>
63+
<p><a asp-action="ForgotPassword" asp-controller="Account">Forgot password?</a></p>
64+
6365
<p>Click <a asp-action="Signup" asp-controller="Account">here</a> to signup</p>
6466
</div>
6567
</div>

Webgentle.BookStore/Webgentle.BookStore/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"Application": {
1515
"LoginPath": "/login",
1616
"AppDomain": "http://localhost:65352/",
17-
"EmailConfirmation": "confirm-email?uid={0}&token={1}"
17+
"EmailConfirmation": "confirm-email?uid={0}&token={1}",
18+
"ForgotPassword": "reset-password?uid={0}&token={1}"
1819
},
1920

2021
"NewBookAlert": {

0 commit comments

Comments
 (0)