Skip to content

Commit e3a6a88

Browse files
committed
Fix Email.razor's default NewEmail
1 parent c5663da commit e3a6a88

File tree

4 files changed

+26
-29
lines changed

4 files changed

+26
-29
lines changed

src/Components/test/E2ETest/ServerRenderingTests/FormHandlingTests/FormWithParentBindingContextTest.cs

-2
Original file line numberDiff line numberDiff line change
@@ -917,9 +917,7 @@ public void CanUseAntiforgeryTokenWithServerInteractivity()
917917
Url = "forms/antiforgery-server-interactive",
918918
FormCssSelector = "form",
919919
InputFieldId = "value",
920-
InputFieldValue = "stranger",
921920
SuppressEnhancedNavigation = true,
922-
Ready = "really-ready",
923921
};
924922
DispatchToFormCore(dispatchToForm);
925923
}

src/Components/test/testassets/Components.TestServer/RazorComponentEndpointsStartup.cs

+11-15
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
7272
InteractiveStreamingRenderingComponent.MapEndpoints(endpoints);
7373

7474
MapEnhancedNavigationEndpoints(endpoints);
75-
76-
endpoints.MapPost("/verify-antiforgery", (
77-
[FromForm] string value,
78-
[FromForm(Name = "__RequestVerificationToken")] string csrfToken) =>
79-
{
80-
// We shouldn't get this far without a valid CSRF token, but we double check it's there.
81-
if (string.IsNullOrEmpty(csrfToken))
82-
{
83-
throw new Exception("Invalid POST to /verify-antiforgery!");
84-
}
85-
86-
return TypedResults.Text($"<p id='pass'>Hello {value}!</p>", "text/html");
87-
});
8875
});
8976
});
9077
}
@@ -167,9 +154,18 @@ private static void MapEnhancedNavigationEndpoints(IEndpointRouteBuilder endpoin
167154
await response.WriteAsync("<html><body><h1>This is a non-Blazor endpoint</h1><p>That's all</p></body></html>");
168155
});
169156

170-
endpoints.MapPost("api/antiforgery-form", ([FromForm] string value) =>
157+
endpoints.MapPost("api/antiforgery-form", (
158+
[FromForm] string value,
159+
[FromForm(Name = "__RequestVerificationToken")] string? inFormCsrfToken,
160+
[FromHeader(Name = "RequestVerificationToken")] string? inHeaderCsrfToken) =>
171161
{
172-
return Results.Ok(value);
162+
// We shouldn't get this far without a valid CSRF token, but we'll double check it's there.
163+
if (string.IsNullOrEmpty(inFormCsrfToken) && string.IsNullOrEmpty(inHeaderCsrfToken))
164+
{
165+
throw new InvalidOperationException("Invalid POST to api/antiforgery-form!");
166+
}
167+
168+
return TypedResults.Text($"<p id='pass'>Hello {value}!</p>", "text/html");
173169
});
174170

175171
static Task PerformRedirection(HttpRequest request, HttpResponse response)

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/Forms/FormRenderedWithServerInteractivityCanUseAntiforgeryToken.razor

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66

77
<h3>FormRenderedWithServerInteractivityCanUseAntiforgeryToken</h3>
88

9-
<form action="verify-antiforgery" method="post" @formname="verify-antiforgery">
9+
<form action="api/antiforgery-form" method="post">
1010
<AntiforgeryToken />
1111
<input type="text" id="value" name="value" />
12-
<input id="send" name="send" type="submit" value="Send" />
12+
@if (HttpContext is null)
13+
{
14+
<input id="send" type="submit" value="Send" />
15+
}
1316
</form>
1417

15-
@if (HttpContext is null)
16-
{
17-
<p id="really-ready">Interactively rendered!</p>
18-
}
19-
2018
@code {
2119
[CascadingParameter]
2220
HttpContext? HttpContext { get; set; }

src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Components/Account/Pages/Manage/Email.razor

+10-5
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,18 @@
7171
email = await UserManager.GetEmailAsync(user);
7272
isEmailConfirmed = await UserManager.IsEmailConfirmedAsync(user);
7373

74-
Input.NewEmail ??= email!;
74+
Input.NewEmail ??= email;
7575
}
7676

7777
private async Task OnValidSubmitAsync()
7878
{
79-
if (Input.NewEmail == email)
79+
if (Input.NewEmail is null || Input.NewEmail == email)
8080
{
8181
RedirectManager.RedirectToCurrentPageWithStatus("Your email is unchanged.", HttpContext);
8282
}
8383

8484
var userId = await UserManager.GetUserIdAsync(user);
85-
var code = await UserManager.GenerateChangeEmailTokenAsync(user, Input.NewEmail);
85+
var code = await UserManager.GenerateChangeEmailTokenAsync(user, Input.NewEmail!);
8686
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
8787
var callbackUrl = NavigationManager.GetUriWithQueryParameters(
8888
NavigationManager.ToAbsoluteUri("Account/ConfirmEmailChange").AbsoluteUri,
@@ -94,14 +94,19 @@
9494

9595
private async Task OnSendEmailVerificationAsync()
9696
{
97+
if (email is null)
98+
{
99+
return;
100+
}
101+
97102
var userId = await UserManager.GetUserIdAsync(user);
98103
var code = await UserManager.GenerateEmailConfirmationTokenAsync(user);
99104
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
100105
var callbackUrl = NavigationManager.GetUriWithQueryParameters(
101106
NavigationManager.ToAbsoluteUri("Account/ConfirmEmail").AbsoluteUri,
102107
new Dictionary<string, object?> { ["userId"] = userId, ["code"] = code });
103108

104-
await EmailSender.SendConfirmationLinkAsync(user, email!, HtmlEncoder.Default.Encode(callbackUrl));
109+
await EmailSender.SendConfirmationLinkAsync(user, email, HtmlEncoder.Default.Encode(callbackUrl));
105110

106111
RedirectManager.RedirectToCurrentPageWithStatus("Verification email sent. Please check your email.", HttpContext);
107112
}
@@ -111,6 +116,6 @@
111116
[Required]
112117
[EmailAddress]
113118
[Display(Name = "New email")]
114-
public string NewEmail { get; set; } = "";
119+
public string? NewEmail { get; set; }
115120
}
116121
}

0 commit comments

Comments
 (0)