Skip to content

Commit 5989d4a

Browse files
committed
Move SignOutManager inside RemoteAuthenticatorViewCore
1 parent edeb548 commit 5989d4a

File tree

8 files changed

+154
-106
lines changed

8 files changed

+154
-106
lines changed

src/Components/Blazor/WebAssembly.Authentication/src/Microsoft.AspNetCore.Components.WebAssembly.Authentication.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Razor">
1+
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<Sdk Name="Yarn.MSBuild" />
44

@@ -12,6 +12,7 @@
1212

1313
<ItemGroup>
1414
<Reference Include="Microsoft.AspNetCore.Components.Authorization" />
15+
<Reference Include="Microsoft.AspNetCore.Components.Web" />
1516
</ItemGroup>
1617

1718
<ItemGroup>

src/Components/Blazor/WebAssembly.Authentication/src/RemoteAuthenticatorViewCore.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,16 @@ public class RemoteAuthenticatorViewCore<TAuthenticationState> : ComponentBase w
9999
/// </summary>
100100
[Inject] public AuthenticationStateProvider AuthenticationProvider { get; set; }
101101

102+
/// <summary>
103+
/// Gets or sets a default <see cref="AuthenticationStateProvider"/> with the current user.
104+
/// </summary>
105+
[Inject] public SignOutSessionStateManager SignOutManager { get; set; }
106+
102107
/// <summary>
103108
/// Gets or sets the <see cref="RemoteAuthenticationApplicationPathsOptions"/> with the paths to different authentication pages.
104109
/// </summary>
105-
[Parameter] public RemoteAuthenticationApplicationPathsOptions ApplicationPaths
110+
[Parameter]
111+
public RemoteAuthenticationApplicationPathsOptions ApplicationPaths
106112
{
107113
get => _applicationPaths ?? RemoteApplicationPathsProvider.ApplicationPaths;
108114
set => _applicationPaths = value;
@@ -249,10 +255,18 @@ private async Task ProcessLogInCallback()
249255

250256
private async Task ProcessLogOut(string returnUrl)
251257
{
258+
if (!await SignOutManager.ValidateSignOutState())
259+
{
260+
var uri = $"{Navigation.ToAbsoluteUri(ApplicationPaths.LogOutFailedPath)}?message={Uri.EscapeDataString("The logout was not initiated from within the page.")}";
261+
Navigation.NavigateTo(uri);
262+
263+
return;
264+
}
265+
252266
AuthenticationState.ReturnUrl = returnUrl;
267+
253268
var state = await AuthenticationProvider.GetAuthenticationStateAsync();
254269
var isauthenticated = state.User.Identity.IsAuthenticated;
255-
256270
if (isauthenticated)
257271
{
258272
var result = await AuthenticationService.SignOutAsync(new RemoteAuthenticationContext<TAuthenticationState> { State = AuthenticationState });

src/Components/Blazor/WebAssembly.Authentication/src/Services/SignOutSessionStateManager.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,13 @@ public class SignOutSessionStateManager
1919
PropertyNameCaseInsensitive = true,
2020
};
2121

22-
/// <summary>
23-
/// Gets the validity of the sign out state.
24-
/// </summary>
25-
public bool ValidSignOutState { get; private set; } = false;
26-
2722
public SignOutSessionStateManager(IJSRuntime jsRuntime) => _jsRuntime = jsRuntime;
2823

2924
/// <summary>
3025
/// Sets up some state in session storage to allow for logouts from within the <see cref="RemoteAuthenticationDefaults.LogoutPath"/> page.
3126
/// </summary>
3227
/// <returns>A <see cref="ValueTask"/> that completes when the state has been saved to session storage.</returns>
33-
public ValueTask SetSignOutState()
28+
public virtual ValueTask SetSignOutState()
3429
{
3530
return _jsRuntime.InvokeVoidAsync(
3631
"sessionStorage.setItem",
@@ -43,16 +38,16 @@ public ValueTask SetSignOutState()
4338
/// logouts from within the <see cref="RemoteAuthenticationDefaults.LogoutPath"/> page.
4439
/// </summary>
4540
/// <returns>A <see cref="ValueTask{bool}"/> that completes when the state has been validated and indicates the validity of the state.</returns>
46-
public async Task<bool> ValidateSignOutState()
41+
public virtual async Task<bool> ValidateSignOutState()
4742
{
4843
var state = await GetSignOutState();
4944
if (state.Local)
5045
{
5146
await ClearSignOutState();
52-
ValidSignOutState = true;
47+
return true;
5348
}
5449

55-
return ValidSignOutState;
50+
return false;
5651
}
5752

5853
private async ValueTask<SignOutState> GetSignOutState()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@using Microsoft.AspNetCore.Components
2+
@using Microsoft.AspNetCore.Components.Web
3+
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication.Infrastructure
4+
@OnSignOut(BeginSignOut)
5+
@code {
6+
7+
[Parameter] public RenderFragment<Func<MouseEventArgs,Task>> OnSignOut { get; set; }
8+
9+
[Inject] public SignOutSessionStateManager SignOutManager { get; set; }
10+
11+
[Inject] public NavigationManager Navigation { get; set; }
12+
13+
[Inject] public IRemoteAuthenticationPathsProvider Paths { get; set; }
14+
15+
public async Task BeginSignOut(MouseEventArgs args)
16+
{
17+
await SignOutManager.SetSignOutState();
18+
Navigation.NavigateTo(Paths.ApplicationPaths.LogOutPath);
19+
}
20+
}

0 commit comments

Comments
 (0)