-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Fix resend email confirmation #14118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
32540d5
Fix resend email confirmation
HaoK 79e68d6
Fix resend email confirmation
HaoK 377f736
Merge branch 'identity-confirm' of https://github.com/aspnet/AspNetCo…
HaoK 97b7b01
Encode the code
HaoK 79dbae2
Fix V4 encoding
HaoK 93bfe1c
Merge branch 'master' into identity-confirm
HaoK dde4850
Merge branch 'identity-confirm' of https://github.com/aspnet/AspNetCo…
HaoK 4364575
Fix using
HaoK be8f860
Update Microsoft.AspNetCore.Identity.UI.netcoreapp.cs
HaoK 1759398
Fix ref
HaoK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/Identity/UI/src/Areas/Identity/Pages/V3/Account/ResendEmailConfirmation.cshtml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@page | ||
@model ResendEmailConfirmationModel | ||
@{ | ||
ViewData["Title"] = "Resend email confirmation"; | ||
} | ||
|
||
<h2>@ViewData["Title"]</h2> | ||
<h4>Enter your email.</h4> | ||
<hr /> | ||
<div class="row"> | ||
<div class="col-md-4"> | ||
<form method="post"> | ||
<div asp-validation-summary="All" class="text-danger"></div> | ||
<div class="form-group"> | ||
<label asp-for="Input.Email"></label> | ||
<input asp-for="Input.Email" class="form-control" /> | ||
<span asp-validation-for="Input.Email" class="text-danger"></span> | ||
</div> | ||
<button type="submit" class="btn btn-default">Resend</button> | ||
</form> | ||
</div> | ||
</div> | ||
|
||
@section Scripts { | ||
<partial name="_ValidationScriptsPartial" /> | ||
} |
106 changes: 106 additions & 0 deletions
106
src/Identity/UI/src/Areas/Identity/Pages/V3/Account/ResendEmailConfirmation.cshtml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Text; | ||
using System.Text.Encodings.Web; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity.UI.Services; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.AspNetCore.WebUtilities; | ||
|
||
namespace Microsoft.AspNetCore.Identity.UI.V3.Pages.Account.Internal | ||
{ | ||
/// <summary> | ||
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
[AllowAnonymous] | ||
[IdentityDefaultUI(typeof(ResendEmailConfirmationModel<>))] | ||
public abstract class ResendEmailConfirmationModel : PageModel | ||
{ | ||
/// <summary> | ||
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
[BindProperty] | ||
public InputModel Input { get; set; } | ||
|
||
/// <summary> | ||
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public class InputModel | ||
{ | ||
/// <summary> | ||
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
[Required] | ||
[EmailAddress] | ||
public string Email { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public virtual void OnGet() => throw new NotImplementedException(); | ||
|
||
/// <summary> | ||
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public virtual Task<IActionResult> OnPostAsync() => throw new NotImplementedException(); | ||
} | ||
|
||
internal class ResendEmailConfirmationModel<TUser> : ResendEmailConfirmationModel where TUser : class | ||
{ | ||
private readonly UserManager<TUser> _userManager; | ||
private readonly IEmailSender _emailSender; | ||
|
||
public ResendEmailConfirmationModel(UserManager<TUser> userManager, IEmailSender emailSender) | ||
{ | ||
_userManager = userManager; | ||
_emailSender = emailSender; | ||
} | ||
|
||
public override void OnGet() | ||
{ | ||
} | ||
|
||
public override async Task<IActionResult> OnPostAsync() | ||
{ | ||
if (!ModelState.IsValid) | ||
{ | ||
return Page(); | ||
} | ||
|
||
var user = await _userManager.FindByEmailAsync(Input.Email); | ||
if (user == null) | ||
{ | ||
ModelState.AddModelError(string.Empty, "Verification email sent. Please check your email."); | ||
return Page(); | ||
} | ||
|
||
var userId = await _userManager.GetUserIdAsync(user); | ||
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); | ||
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); | ||
var callbackUrl = Url.Page( | ||
"/Account/ConfirmEmail", | ||
pageHandler: null, | ||
values: new { userId = userId, code = code }, | ||
protocol: Request.Scheme); | ||
await _emailSender.SendEmailAsync( | ||
Input.Email, | ||
"Confirm your email", | ||
$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); | ||
|
||
ModelState.AddModelError(string.Empty, "Verification email sent. Please check your email."); | ||
return Page(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/Identity/UI/src/Areas/Identity/Pages/V4/Account/ResendEmailConfirmation.cshtml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@page | ||
@model ResendEmailConfirmationModel | ||
@{ | ||
ViewData["Title"] = "Resend email confirmation"; | ||
} | ||
|
||
<h1>@ViewData["Title"]</h1> | ||
<h4>Enter your email.</h4> | ||
<hr /> | ||
<div class="row"> | ||
<div class="col-md-4"> | ||
<form method="post"> | ||
<div asp-validation-summary="All" class="text-danger"></div> | ||
<div class="form-group"> | ||
<label asp-for="Input.Email"></label> | ||
<input asp-for="Input.Email" class="form-control" /> | ||
<span asp-validation-for="Input.Email" class="text-danger"></span> | ||
</div> | ||
<button type="submit" class="btn btn-primary">Resend</button> | ||
</form> | ||
</div> | ||
</div> | ||
|
||
@section Scripts { | ||
<partial name="_ValidationScriptsPartial" /> | ||
} |
106 changes: 106 additions & 0 deletions
106
src/Identity/UI/src/Areas/Identity/Pages/V4/Account/ResendEmailConfirmation.cshtml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Text; | ||
using System.Text.Encodings.Web; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity.UI.Services; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.AspNetCore.WebUtilities; | ||
|
||
namespace Microsoft.AspNetCore.Identity.UI.V4.Pages.Account.Internal | ||
{ | ||
/// <summary> | ||
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
[AllowAnonymous] | ||
[IdentityDefaultUI(typeof(ResendEmailConfirmationModel<>))] | ||
public abstract class ResendEmailConfirmationModel : PageModel | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice if we could unify this considering that it seems to just be a copy of the V3 one, but if there's not an existing structure for making those two versions share code I can see it being not worth the effort. |
||
{ | ||
/// <summary> | ||
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
[BindProperty] | ||
public InputModel Input { get; set; } | ||
|
||
/// <summary> | ||
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public class InputModel | ||
{ | ||
/// <summary> | ||
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
[Required] | ||
[EmailAddress] | ||
public string Email { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public virtual void OnGet() => throw new NotImplementedException(); | ||
|
||
/// <summary> | ||
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public virtual Task<IActionResult> OnPostAsync() => throw new NotImplementedException(); | ||
} | ||
|
||
internal class ResendEmailConfirmationModel<TUser> : ResendEmailConfirmationModel where TUser : class | ||
{ | ||
private readonly UserManager<TUser> _userManager; | ||
private readonly IEmailSender _emailSender; | ||
|
||
public ResendEmailConfirmationModel(UserManager<TUser> userManager, IEmailSender emailSender) | ||
{ | ||
_userManager = userManager; | ||
_emailSender = emailSender; | ||
} | ||
|
||
public override void OnGet() | ||
{ | ||
} | ||
|
||
public override async Task<IActionResult> OnPostAsync() | ||
{ | ||
if (!ModelState.IsValid) | ||
{ | ||
return Page(); | ||
} | ||
|
||
var user = await _userManager.FindByEmailAsync(Input.Email); | ||
if (user == null) | ||
{ | ||
ModelState.AddModelError(string.Empty, "Verification email sent. Please check your email."); | ||
return Page(); | ||
} | ||
|
||
var userId = await _userManager.GetUserIdAsync(user); | ||
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); | ||
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); | ||
var callbackUrl = Url.Page( | ||
"/Account/ConfirmEmail", | ||
pageHandler: null, | ||
values: new { userId = userId, code = code }, | ||
protocol: Request.Scheme); | ||
await _emailSender.SendEmailAsync( | ||
Input.Email, | ||
"Confirm your email", | ||
$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); | ||
|
||
ModelState.AddModelError(string.Empty, "Verification email sent. Please check your email."); | ||
return Page(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.