Skip to content

Commit f10d9df

Browse files
reset password in asp.net core
1 parent 8a944a2 commit f10d9df

File tree

6 files changed

+113
-1
lines changed

6 files changed

+113
-1
lines changed

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

+33
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,38 @@ public async Task<IActionResult> ForgotPassword(ForgotPasswordModel model)
187187
}
188188
return View(model);
189189
}
190+
191+
[AllowAnonymous, HttpGet("reset-password")]
192+
public IActionResult ResetPassword(string uid, string token)
193+
{
194+
ResetPasswordModel resetPasswordModel = new ResetPasswordModel
195+
{
196+
Token = token,
197+
UserId = uid
198+
};
199+
return View(resetPasswordModel);
200+
}
201+
202+
[AllowAnonymous, HttpPost("reset-password")]
203+
public async Task<IActionResult> ResetPassword(ResetPasswordModel model)
204+
{
205+
if (ModelState.IsValid)
206+
{
207+
model.Token = model.Token.Replace(' ', '+');
208+
var result = await _accountRepository.ResetPasswordAsync(model);
209+
if (result.Succeeded)
210+
{
211+
ModelState.Clear();
212+
model.IsSuccess = true;
213+
return View(model);
214+
}
215+
216+
foreach (var error in result.Errors)
217+
{
218+
ModelState.AddModelError("", error.Description);
219+
}
220+
}
221+
return View(model);
222+
}
190223
}
191224
}

Webgentle.BookStore/Webgentle.BookStore/EmailTemplate/ForgotPassword.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
Hello {{UserName}}, <br />
1010
Click on the following link to reset your password. <br /><br />
1111

12-
<a href="{{Link}}">Verify email</a>
12+
<a href="{{Link}}">Reset password</a>
1313
</body>
1414
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 ResetPasswordModel
10+
{
11+
[Required]
12+
public string UserId { get; set; }
13+
14+
[Required]
15+
public string Token { get; set; }
16+
17+
[Required, DataType(DataType.Password)]
18+
public string NewPassword { get; set; }
19+
20+
[Required, DataType(DataType.Password)]
21+
[Compare("NewPassword")]
22+
public string ConfirmNewPassword { get; set; }
23+
24+
public bool IsSuccess { get; set; }
25+
}
26+
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ public async Task<IdentityResult> ConfirmEmailAsync(string uid, string token)
9494
return await _userManager.ConfirmEmailAsync(await _userManager.FindByIdAsync(uid), token);
9595
}
9696

97+
public async Task<IdentityResult> ResetPasswordAsync(ResetPasswordModel model)
98+
{
99+
return await _userManager.ResetPasswordAsync(await _userManager.FindByIdAsync(model.UserId), model.Token, model.NewPassword);
100+
}
101+
97102
private async Task SendEmailConfirmationEmail(ApplicationUser user, string token)
98103
{
99104
string appDomain = _configuration.GetSection("Application:AppDomain").Value;

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

+2
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ public interface IAccountRepository
2121
Task GenerateEmailConfirmationTokenAsync(ApplicationUser user);
2222

2323
Task GenerateForgotPasswordTokenAsync(ApplicationUser user);
24+
25+
Task<IdentityResult> ResetPasswordAsync(ResetPasswordModel model);
2426
}
2527
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
@model Webgentle.BookStore.Models.ResetPasswordModel
2+
3+
@{
4+
ViewData["Title"] = "ResetPassword";
5+
}
6+
<div class="container">
7+
8+
<h1 class="display-4">Reset password</h1>
9+
10+
<hr />
11+
<div class="row">
12+
<div class="offset-4 col-md-4">
13+
@if (Model?.IsSuccess == true)
14+
{
15+
<div class="alert alert-success" role="alert">
16+
<p>
17+
You have updated your password successfully.
18+
</p>
19+
</div>
20+
}
21+
else
22+
{
23+
<form asp-action="ResetPassword">
24+
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
25+
<input type="hidden" asp-for="UserId" class="form-control" />
26+
<input type="hidden" asp-for="Token" class="form-control" />
27+
<div class="form-group">
28+
<label asp-for="NewPassword" class="control-label"></label>
29+
<input asp-for="NewPassword" class="form-control" />
30+
<span asp-validation-for="NewPassword" class="text-danger"></span>
31+
</div>
32+
<div class="form-group">
33+
<label asp-for="ConfirmNewPassword" class="control-label"></label>
34+
<input asp-for="ConfirmNewPassword" class="form-control" />
35+
<span asp-validation-for="ConfirmNewPassword" class="text-danger"></span>
36+
</div>
37+
<div class="form-group">
38+
<input type="submit" value="Reset password" class="btn btn-primary" />
39+
</div>
40+
</form>
41+
}
42+
43+
</div>
44+
</div>
45+
46+
</div>

0 commit comments

Comments
 (0)