Skip to content

#3897 Fix error catching in blazor ApplicationContextManagers #3903

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
11 changes: 7 additions & 4 deletions Source/Csla.AspNetCore/Blazor/ApplicationContextManagerBlazor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ApplicationContextManagerBlazor(IHttpContextAccessor hca, AuthenticationS
ActiveCircuitState = activeCircuitState;
CurrentPrincipal = UnauthenticatedPrincipal;
AuthenticationStateProvider.AuthenticationStateChanged += AuthenticationStateProvider_AuthenticationStateChanged;
InitializeUser();
_ = InitializeUser();
}

private IPrincipal CurrentPrincipal { get; set; }
Expand All @@ -63,21 +63,24 @@ public ApplicationContextManagerBlazor(IHttpContextAccessor hca, AuthenticationS
/// </summary>
protected HttpContext HttpContext { get; set; }

private void InitializeUser()
private async Task InitializeUser()
{
if (ActiveCircuitState.CircuitExists)
{
Task<AuthenticationState> task;
try
{
task = AuthenticationStateProvider.GetAuthenticationStateAsync();
await task;
}
catch (InvalidOperationException ex)
{
task = Task.FromResult(new AuthenticationState(UnauthenticatedPrincipal));

string message = ex.Message;
if (message.Contains(nameof(AuthenticationStateProvider.GetAuthenticationStateAsync))
&& message.Contains(nameof(IHostEnvironmentAuthenticationStateProvider.SetAuthenticationState)))
//see ms error https://github.com/dotnet/aspnetcore/blob/87e324a61dcd15db4086b8a8ca7bd74ca1e0a513/src/Components/Server/src/Circuits/ServerAuthenticationStateProvider.cs#L16
//not much safe to test on except the error type and the use of this method name in message.
if (message.Contains(nameof(AuthenticationStateProvider.GetAuthenticationStateAsync)))
{
SetHostPrincipal(task);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//-----------------------------------------------------------------------
using Csla.Core;
using Microsoft.AspNetCore.Components.Authorization;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;

Expand Down Expand Up @@ -52,22 +53,25 @@ public ApplicationContextManagerInMemory(AuthenticationStateProvider authenticat
ActiveCircuitState = activeCircuitState;
CurrentPrincipal = UnauthenticatedPrincipal;
AuthenticationStateProvider.AuthenticationStateChanged += AuthenticationStateProvider_AuthenticationStateChanged;
InitializeUser();
_ = InitializeUser();
}

private void InitializeUser()
private async Task InitializeUser()
{
Task<AuthenticationState> task = default;
try
{
task = AuthenticationStateProvider.GetAuthenticationStateAsync();
await task;
}
catch (InvalidOperationException ex)
{
task = Task.FromResult(new AuthenticationState(UnauthenticatedPrincipal));

string message = ex.Message;
if (message.Contains(nameof(AuthenticationStateProvider.GetAuthenticationStateAsync))
&& message.Contains(nameof(IHostEnvironmentAuthenticationStateProvider.SetAuthenticationState)))
//see ms error https://github.com/dotnet/aspnetcore/blob/87e324a61dcd15db4086b8a8ca7bd74ca1e0a513/src/Components/Server/src/Circuits/ServerAuthenticationStateProvider.cs#L16
//not much safe to test on except the error type and the use of this method name in message.
if (message.Contains(nameof(AuthenticationStateProvider.GetAuthenticationStateAsync)))
{
SetHostPrincipal(task);
}
Expand Down
Loading