Skip to content

Commit 75133d9

Browse files
committed
Add IdentityRedirectManager
1 parent 4baa37a commit 75133d9

31 files changed

+215
-158
lines changed

src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Components/Identity/ExternalLoginPicker.razor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
@using Microsoft.AspNetCore.Identity
33
@using BlazorWeb_CSharp.Components.Pages.Account
44
@using BlazorWeb_CSharp.Data
5+
@using BlazorWeb_CSharp.Identity
56

67
@inject SignInManager<ApplicationUser> SignInManager
7-
@inject NavigationManager NavigationManager
8+
@inject IdentityRedirectManager RedirectManager
89

910
@if ((_externalLogins?.Count ?? 0) == 0)
1011
{

src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Components/Identity/LogoutForm.razor

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
@using Microsoft.AspNetCore.Identity
22
@using BlazorWeb_CSharp.Data
3+
@using BlazorWeb_CSharp.Identity
34

45
@inject SignInManager<ApplicationUser> SignInManager
56
@inject NavigationManager NavigationManager
7+
@inject IdentityRedirectManager RedirectManager
68

79
<form @onsubmit="OnSubmitAsync" @formname="logout" method="post" @attributes="AdditionalAttributes">
810
<AntiforgeryToken />
@@ -26,7 +28,7 @@
2628
if (SignInManager.IsSignedIn(user))
2729
{
2830
await SignInManager.SignOutAsync();
29-
NavigationManager.RedirectTo(ReturnUrl ?? "/");
31+
RedirectManager.RedirectTo(ReturnUrl ?? "/");
3032
}
3133
}
3234
}

src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Components/Identity/StatusMessage.razor

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
@{
2-
var message = Message ?? MessageFromQuery;
1+
@using BlazorWeb_CSharp.Identity
2+
3+
@{
4+
var message = Message ?? MessageFromCookie;
5+
6+
if (MessageFromCookie is not null)
7+
{
8+
HttpContext.Response.Cookies.Delete(IdentityRedirectManager.StatusCookieName);
9+
}
310
}
411

512
@if (!string.IsNullOrEmpty(message))
@@ -15,6 +22,8 @@
1522
[Parameter]
1623
public string? Message { get; set; }
1724

18-
[SupplyParameterFromQuery(Name = "Message")]
19-
public string? MessageFromQuery { get; set; }
25+
[CascadingParameter]
26+
private HttpContext HttpContext { get; set; } = default!;
27+
28+
private string? MessageFromCookie => HttpContext.Request.Cookies[IdentityRedirectManager.StatusCookieName];
2029
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
@page "/Account/ConfirmEmail"
22

33
@using System.Text
4-
@using BlazorWeb_CSharp.Data
54
@using Microsoft.AspNetCore.Identity
65
@using Microsoft.AspNetCore.WebUtilities
6+
@using BlazorWeb_CSharp.Data
7+
@using BlazorWeb_CSharp.Identity
78

89
@inject UserManager<ApplicationUser> UserManager
9-
@inject NavigationManager NavigationManager
10+
@inject IdentityRedirectManager RedirectManager
1011

1112
<PageTitle>Confirm email</PageTitle>
1213

@@ -26,7 +27,7 @@
2627
{
2728
if (UserId == null || Code == null)
2829
{
29-
NavigationManager.RedirectTo("/");
30+
RedirectManager.RedirectTo("/");
3031
}
3132
else
3233
{

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
@using Microsoft.AspNetCore.Identity
55
@using Microsoft.AspNetCore.WebUtilities
66
@using BlazorWeb_CSharp.Data
7+
@using BlazorWeb_CSharp.Identity
78

89
@inject UserManager<ApplicationUser> UserManager
910
@inject SignInManager<ApplicationUser> SignInManager
1011
@inject UserAccessor UserAccessor
11-
@inject NavigationManager NavigationManager
12+
@inject IdentityRedirectManager RedirectManager
1213

1314
<PageTitle>Confirm email change</PageTitle>
1415

@@ -33,9 +34,8 @@
3334
{
3435
if (UserId is null || Email is null || Code is null)
3536
{
36-
NavigationManager.RedirectTo(
37-
"/Account/Login",
38-
new() { ["Message"] = "Error: Invalid email change confirmation link." });
37+
RedirectManager.RedirectToWithStatus(
38+
"/Account/Login", "Error: Invalid email change confirmation link.");
3939
return;
4040
}
4141

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
@using Microsoft.AspNetCore.Identity.UI.Services
99
@using Microsoft.AspNetCore.WebUtilities
1010
@using BlazorWeb_CSharp.Data
11+
@using BlazorWeb_CSharp.Identity
1112

1213
@inject SignInManager<ApplicationUser> SignInManager
1314
@inject UserManager<ApplicationUser> UserManager
1415
@inject IUserStore<ApplicationUser> UserStore
1516
@inject IEmailSender EmailSender
1617
@inject NavigationManager NavigationManager
18+
@inject IdentityRedirectManager RedirectManager
1719
@inject ILogger<ExternalLogin> Logger
1820

1921
@{
@@ -77,18 +79,14 @@
7779

7880
if (RemoteError is not null)
7981
{
80-
NavigationManager.RedirectTo(
81-
"/Account/Login",
82-
new() { ["Message"] = "Error from external provider: " + RemoteError });
82+
RedirectManager.RedirectToWithStatus("/Account/Login", "Error from external provider: " + RemoteError);
8383
return;
8484
}
8585

8686
var externalLoginInfo = await SignInManager.GetExternalLoginInfoAsync();
8787
if (externalLoginInfo is null)
8888
{
89-
NavigationManager.RedirectTo(
90-
"/Account/Login",
91-
new() { ["Message"] = "Error loading external login information." });
89+
RedirectManager.RedirectToWithStatus("/Account/Login", "Error loading external login information.");
9290
return;
9391
}
9492

@@ -105,7 +103,7 @@
105103

106104
// We should only reach this page via the login callback, so redirect back to
107105
// the login page if we get here some other way.
108-
NavigationManager.RedirectTo("/Account/Login");
106+
RedirectManager.RedirectTo("/Account/Login");
109107
return;
110108
}
111109
}
@@ -124,13 +122,13 @@
124122
"{Name} logged in with {LoginProvider} provider.",
125123
_externalLoginInfo.Principal.Identity?.Name,
126124
_externalLoginInfo.LoginProvider);
127-
NavigationManager.RedirectTo(ReturnUrl);
125+
RedirectManager.RedirectTo(ReturnUrl);
128126
return;
129127
}
130128

131129
if (result.IsLockedOut)
132130
{
133-
NavigationManager.RedirectTo("/Account/Lockout");
131+
RedirectManager.RedirectTo("/Account/Lockout");
134132
return;
135133
}
136134

@@ -169,12 +167,12 @@
169167
// If account confirmation is required, we need to show the link if we don't have a real email sender
170168
if (UserManager.Options.SignIn.RequireConfirmedAccount)
171169
{
172-
NavigationManager.RedirectTo("/Account/RegisterConfirmation", new() { ["Email"] = Input.Email });
170+
RedirectManager.RedirectTo("/Account/RegisterConfirmation", new() { ["Email"] = Input.Email });
173171
return;
174172
}
175173

176174
await SignInManager.SignInAsync(user, isPersistent: false, _externalLoginInfo.LoginProvider);
177-
NavigationManager.RedirectTo(ReturnUrl);
175+
RedirectManager.RedirectTo(ReturnUrl);
178176
return;
179177
}
180178
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
@page "/Account/ForgotPassword"
22

3-
@using System.ComponentModel.DataAnnotations;
4-
@using System.Text;
5-
@using System.Text.Encodings.Web;
6-
@using Microsoft.AspNetCore.Identity;
7-
@using Microsoft.AspNetCore.Identity.UI.Services;
8-
@using Microsoft.AspNetCore.WebUtilities;
9-
@using BlazorWeb_CSharp.Data;
3+
@using System.ComponentModel.DataAnnotations
4+
@using System.Text
5+
@using System.Text.Encodings.Web
6+
@using Microsoft.AspNetCore.Identity
7+
@using Microsoft.AspNetCore.Identity.UI.Services
8+
@using Microsoft.AspNetCore.WebUtilities
9+
@using BlazorWeb_CSharp.Data
10+
@using BlazorWeb_CSharp.Identity
1011

1112
@inject NavigationManager NavigationManager
13+
@inject IdentityRedirectManager RedirectManager
1214
@inject UserManager<ApplicationUser> UserManager
1315
@inject IEmailSender EmailSender
1416

@@ -43,7 +45,7 @@
4345
if (user is null || !(await UserManager.IsEmailConfirmedAsync(user)))
4446
{
4547
// Don't reveal that the user does not exist or is not confirmed
46-
NavigationManager.RedirectTo("/Account/ForgotPasswordConfirmation");
48+
RedirectManager.RedirectTo("/Account/ForgotPasswordConfirmation");
4749
return;
4850
}
4951

@@ -60,7 +62,7 @@
6062
"Reset Password",
6163
$"Please reset your password by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
6264

63-
NavigationManager.RedirectTo("/Account/ForgotPasswordConfirmation");
65+
RedirectManager.RedirectTo("/Account/ForgotPasswordConfirmation");
6466
}
6567

6668
private sealed class InputModel

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
@using System.ComponentModel.DataAnnotations
44
@using System.Text
5-
@using BlazorWeb_CSharp.Data
65
@using Microsoft.AspNetCore.Authentication
76
@using Microsoft.AspNetCore.Identity
87
@using Microsoft.AspNetCore.WebUtilities
8+
@using BlazorWeb_CSharp.Data
9+
@using BlazorWeb_CSharp.Identity
910

1011
@inject SignInManager<ApplicationUser> SignInManager
1112
@inject ILogger<Login> Logger
1213
@inject NavigationManager NavigationManager
14+
@inject IdentityRedirectManager RedirectManager
1315

1416
<PageTitle>Log in</PageTitle>
1517

@@ -110,18 +112,18 @@
110112
if (result.Succeeded)
111113
{
112114
Logger.LogInformation("User logged in.");
113-
NavigationManager.RedirectTo(ReturnUrl);
115+
RedirectManager.RedirectTo(ReturnUrl);
114116
}
115117
if (result.RequiresTwoFactor)
116118
{
117-
NavigationManager.RedirectTo(
119+
RedirectManager.RedirectTo(
118120
"/Account/LoginWith2fa",
119121
new() { ["ReturnUrl"] = ReturnUrl, ["RememberMe"] = Input.RememberMe });
120122
}
121123
if (result.IsLockedOut)
122124
{
123125
Logger.LogWarning("User account locked out.");
124-
NavigationManager.RedirectTo("/Account/Lockout");
126+
RedirectManager.RedirectTo("/Account/Lockout");
125127
}
126128
else
127129
{

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
@page "/Account/LoginWith2fa"
22

33
@using System.ComponentModel.DataAnnotations
4-
@using System.Text.Encodings.Web
54
@using Microsoft.AspNetCore.Identity
65
@using BlazorWeb_CSharp.Data
6+
@using BlazorWeb_CSharp.Identity
77

88
@inject SignInManager<ApplicationUser> SignInManager
99
@inject UserManager<ApplicationUser> UserManager
10-
@inject NavigationManager NavigationManager
10+
@inject IdentityRedirectManager RedirectManager
1111
@inject ILogger<LoginWith2fa> Logger
1212

1313
<PageTitle>Two-factor authentication</PageTitle>
@@ -42,7 +42,7 @@
4242
</div>
4343
<p>
4444
Don't have access to your authenticator device? You can
45-
<a id="recovery-code-login" href="/Account/LoginWithRecoveryCode?ReturnUrl=@HtmlEncoder.Default.Encode(ReturnUrl ?? "/")">log in with a recovery code</a>.
45+
<a id="recovery-code-login" href="/Account/LoginWithRecoveryCode?ReturnUrl=@ReturnUrl">log in with a recovery code</a>.
4646
</p>
4747

4848
@code {
@@ -61,6 +61,7 @@
6161
protected override async Task OnInitializedAsync()
6262
{
6363
Input ??= new();
64+
ReturnUrl ??= "/";
6465

6566
var user = await SignInManager.GetTwoFactorAuthenticationUserAsync();
6667
if (user is null)
@@ -80,12 +81,12 @@
8081
if (result.Succeeded)
8182
{
8283
Logger.LogInformation("User with ID '{UserId}' logged in with 2fa.", _user.Id);
83-
NavigationManager.RedirectTo(ReturnUrl ?? "/");
84+
RedirectManager.RedirectTo(ReturnUrl ?? "/");
8485
}
8586
else if (result.IsLockedOut)
8687
{
8788
Logger.LogWarning("User with ID '{UserId}' account locked out.", _user.Id);
88-
NavigationManager.RedirectTo("/Account/Lockout");
89+
RedirectManager.RedirectTo("/Account/Lockout");
8990
}
9091
else
9192
{

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
@using Microsoft.AspNetCore.Identity
55
@using Microsoft.AspNetCore.Mvc
66
@using BlazorWeb_CSharp.Data
7+
@using BlazorWeb_CSharp.Identity
78

89
@inject SignInManager<ApplicationUser> SignInManager
910
@inject UserManager<ApplicationUser> UserManager
10-
@inject NavigationManager NavigationManager
11+
@inject IdentityRedirectManager RedirectManager
1112
@inject ILogger<LoginWithRecoveryCode> Logger
1213

1314
<PageTitle>Recovery code verification</PageTitle>
@@ -69,12 +70,12 @@
6970
if (result.Succeeded)
7071
{
7172
Logger.LogInformation("User with ID '{UserId}' logged in with a recovery code.", _user.Id);
72-
NavigationManager.RedirectTo(ReturnUrl ?? "/");
73+
RedirectManager.RedirectTo(ReturnUrl ?? "/");
7374
}
7475
if (result.IsLockedOut)
7576
{
7677
Logger.LogWarning("User account locked out.");
77-
NavigationManager.RedirectTo("/Account/Lockout");
78+
RedirectManager.RedirectTo("/Account/Lockout");
7879
}
7980
else
8081
{

0 commit comments

Comments
 (0)