Skip to content

Commit

Permalink
MarimerLLC#3990 Only for the in memory ContextManager, reverting back…
Browse files Browse the repository at this point in the history
… to allowing SetUser for purely Blazor Server Interactive code to be able to set the ClaimsPrincipal into app context during live circuit. (MarimerLLC#3993)
  • Loading branch information
swegele committed May 30, 2024
1 parent 2151a19 commit 56d9f6d
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions Source/Csla.AspNetCore/Blazor/ApplicationContextManagerInMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,42 @@ public IPrincipal GetUser()
}

/// <summary>
/// Not supported in Blazor.
/// Attempts to set the current principal on the registered
/// IHostEnvironmentAuthenticationStateProvider service.
/// </summary>
/// <param name="principal">Principal object.</param>
public virtual void SetUser(IPrincipal principal) => throw new NotSupportedException(nameof(SetUser));
public virtual void SetUser(IPrincipal principal)
{
if (!ReferenceEquals(CurrentPrincipal, principal))
{
if (ActiveCircuitState.CircuitExists)
{
if (principal is ClaimsPrincipal claimsPrincipal)
{
SetHostPrincipal(Task.FromResult(new AuthenticationState(claimsPrincipal)));
}
else
{
throw new ArgumentException("typeof(principal) != ClaimsPrincipal");
}
}
else if (HttpContext is not null)
{
HttpContext.User = (ClaimsPrincipal)principal;
}
else
{
throw new InvalidOperationException("HttpContext==null, !CircuitExists");
}
CurrentPrincipal = principal;
}
}

private void SetHostPrincipal(Task<AuthenticationState> task)
{
if (AuthenticationStateProvider is IHostEnvironmentAuthenticationStateProvider hostProvider)
hostProvider.SetAuthenticationState(task);
}

/// <summary>
/// Gets the local context.
Expand Down

0 comments on commit 56d9f6d

Please sign in to comment.