Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cb699a9

Browse files
Email confirmation
1 parent 6d6977e commit cb699a9

File tree

5 files changed

+87
-12
lines changed

5 files changed

+87
-12
lines changed

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public async Task<IActionResult> Signup(SignUpUserModel userModel)
4242
}
4343

4444
ModelState.Clear();
45-
return View();
45+
return RedirectToAction("ConfirmEmail",new{email=userModel.Email});
4646
}
4747

4848
return View(userModel);
@@ -120,21 +120,47 @@ public async Task<IActionResult> ChangePassword(ChangePasswordModel model)
120120
}
121121

122122
[HttpGet("confirm-email")]
123-
public async Task<IActionResult> ConfirmEmail(string uid, string token)
123+
public async Task<IActionResult> ConfirmEmail(string uid, string token,stringemail)
124124
{
125+
EmailConfirmModel model = new EmailConfirmModel
126+
{
127+
Email = email
128+
};
125129

126130
if (!string.IsNullOrEmpty(uid) && !string.IsNullOrEmpty(token))
127131
{
128132
token = token.Replace(' ', '+');
129133
var result = await _accountRepository.ConfirmEmailAsync(uid, token);
130134
if (result.Succeeded)
131135
{
132-
ViewBag.IsSuccess = true;
136+
model.EmailVerified = true;
133137
}
134138
}
135139

136-
return View();
140+
return View(model);
141+
}
137142

143+
[HttpPost("confirm-email")]
144+
public async Task<IActionResult> ConfirmEmail(EmailConfirmModel model)
145+
{
146+
var user = await _accountRepository.GetUserByEmailAsync(model.Email);
147+
if (user != null)
148+
{
149+
if (user.EmailConfirmed)
150+
{
151+
model.EmailVerified = true;
152+
return View(model);
153+
}
154+
155+
await _accountRepository.GenerateEmailConfirmationTokenAsync(user);
156+
model.EmailSent = true;
157+
ModelState.Clear();
158+
}
159+
else
160+
{
161+
ModelState.AddModelError("", "Something went wrong.");
162+
}
163+
return View(model);
138164
}
139165
}
140166
}
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.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace Webgentle.BookStore.Models
7+
{
8+
public class EmailConfirmModel
9+
{
10+
public string Email { get; set; }
11+
public bool IsConfirmed { get; set; }
12+
public bool EmailSent { get; set; }
13+
public bool EmailVerified { get; set; }
14+
}
15+
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public AccountRepository(UserManager<ApplicationUser> userManager,
3030
_configuration = configuration;
3131
}
3232

33+
public async Task<ApplicationUser> GetUserByEmailAsync(string email)
34+
{
35+
return await _userManager.FindByEmailAsync(email);
36+
}
37+
3338
public async Task<IdentityResult> CreateUserAsync(SignUpUserModel userModel)
3439
{
3540
var user = new ApplicationUser()
@@ -43,15 +48,20 @@ public async Task<IdentityResult> CreateUserAsync(SignUpUserModel userModel)
4348
var result = await _userManager.CreateAsync(user, userModel.Password);
4449
if (result.Succeeded)
4550
{
46-
var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
47-
if (!string.IsNullOrEmpty(token))
48-
{
49-
await SendEmailConfirmationEmail(user, token);
50-
}
51+
await GenerateEmailConfirmationTokenAsync(user);
5152
}
5253
return result;
5354
}
5455

56+
public async Task GenerateEmailConfirmationTokenAsync(ApplicationUser user)
57+
{
58+
var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
59+
if (!string.IsNullOrEmpty(token))
60+
{
61+
await SendEmailConfirmationEmail(user, token);
62+
}
63+
}
64+
5565
public async Task<SignInResult> PasswordSignInAsync(SignInModel signInModel)
5666
{
5767
return await _signInManager.PasswordSignInAsync(signInModel.Email, signInModel.Password, signInModel.RememberMe, false);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace Webgentle.BookStore.Repository
66
{
77
public interface IAccountRepository
88
{
9+
Task<ApplicationUser> GetUserByEmailAsync(string email);
10+
911
Task<IdentityResult> CreateUserAsync(SignUpUserModel userModel);
1012

1113
Task<SignInResult> PasswordSignInAsync(SignInModel signInModel);
@@ -15,5 +17,7 @@ public interface IAccountRepository
1517
Task<IdentityResult> ChangePasswordAsync(ChangePasswordModel model);
1618

1719
Task<IdentityResult> ConfirmEmailAsync(string uid, string token);
20+
21+
Task GenerateEmailConfirmationTokenAsync(ApplicationUser user);
1822
}
1923
}

‎Webgentle.BookStore/Webgentle.BookStore/Views/Account/ConfirmEmail.cshtml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
1-
1+
@model EmailConfirmModel
22
@{
33
ViewData["Title"] = "Confirm email";
44
}
55
<div class="container">
66
<div class="row text-center">
77
<div class="col-md-4">
8-
@if (ViewBag.IsSuccess == true)
8+
</div>
9+
<div class="col-md-4">
10+
@if (Model.EmailVerified == true)
911
{
1012
<div class="alert alert-success" role="alert">
1113
<p>Your email has been verified successfully.</p>
1214
</div>
1315
}
1416
else
1517
{
16-
<p>Something went wrong or your token has been expired.</p>
18+
if (Model.EmailSent)
19+
{
20+
<p>Email sent on your registered email id.</p>
21+
}
22+
23+
else
24+
{
25+
<p>We have sent an email on your email id. Click on the link to login.</p>
26+
}
27+
28+
<form asp-action="ConfirmEmail">
29+
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
30+
<input type="hidden" asp-for="Email" />
31+
32+
<p>Did not get any email of deleted by mistake?</p>
33+
<div class="form-group">
34+
<input type="submit" value="Resend confirmation email" class="btn btn-primary" />
35+
</div>
36+
</form>
1737
}
1838
</div>
1939
</div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /