Skip to content

Commit 3fa5b5f

Browse files
Use HttpContext to get user, then fall back to AuthenticationStateProvider (#3968)
* #3964 Use HttpContext to get user, then fall back to AuthenticationStateProvider * Add missing using statement * #3964 Remove set user functionality as it doesn't work in Blazor
1 parent e49b961 commit 3fa5b5f

File tree

2 files changed

+31
-95
lines changed

2 files changed

+31
-95
lines changed

Source/Csla.AspNetCore/Blazor/ApplicationContextManagerBlazor.cs

Lines changed: 10 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -65,40 +65,26 @@ public ApplicationContextManagerBlazor(IHttpContextAccessor hca, AuthenticationS
6565

6666
private async Task InitializeUser()
6767
{
68-
if (ActiveCircuitState.CircuitExists)
68+
if (HttpContext != null)
69+
{
70+
var user = HttpContext.User;
71+
if (user != null)
72+
CurrentPrincipal = user;
73+
}
74+
else
6975
{
7076
Task<AuthenticationState> task;
7177
try
7278
{
7379
task = AuthenticationStateProvider.GetAuthenticationStateAsync();
7480
await task;
7581
}
76-
catch (InvalidOperationException ex)
82+
catch (InvalidOperationException)
7783
{
7884
task = Task.FromResult(new AuthenticationState(UnauthenticatedPrincipal));
79-
80-
string message = ex.Message;
81-
//see ms error https://github.com/dotnet/aspnetcore/blob/87e324a61dcd15db4086b8a8ca7bd74ca1e0a513/src/Components/Server/src/Circuits/ServerAuthenticationStateProvider.cs#L16
82-
//not much safe to test on except the error type and the use of this method name in message.
83-
if (message.Contains(nameof(AuthenticationStateProvider.GetAuthenticationStateAsync)))
84-
{
85-
SetHostPrincipal(task);
86-
}
87-
else
88-
{
89-
throw;
90-
}
9185
}
9286
AuthenticationStateProvider_AuthenticationStateChanged(task);
9387
}
94-
else if (HttpContext is not null)
95-
{
96-
CurrentPrincipal = HttpContext.User;
97-
}
98-
else
99-
{
100-
throw new InvalidOperationException("HttpContext==null, !CircuitExists");
101-
}
10288
}
10389

10490
private void AuthenticationStateProvider_AuthenticationStateChanged(Task<AuthenticationState> task)
@@ -144,42 +130,10 @@ public IPrincipal GetUser()
144130
}
145131

146132
/// <summary>
147-
/// Attempts to set the current principal on the registered
148-
/// IHostEnvironmentAuthenticationStateProvider service.
133+
/// Not supported in Blazor.
149134
/// </summary>
150135
/// <param name="principal">Principal object.</param>
151-
public virtual void SetUser(IPrincipal principal)
152-
{
153-
if (!ReferenceEquals(CurrentPrincipal, principal))
154-
{
155-
if (ActiveCircuitState.CircuitExists)
156-
{
157-
if (principal is ClaimsPrincipal claimsPrincipal)
158-
{
159-
SetHostPrincipal(Task.FromResult(new AuthenticationState(claimsPrincipal)));
160-
}
161-
else
162-
{
163-
throw new ArgumentException("typeof(principal) != ClaimsPrincipal");
164-
}
165-
}
166-
else if (HttpContext is not null)
167-
{
168-
HttpContext.User = (ClaimsPrincipal)principal;
169-
}
170-
else
171-
{
172-
throw new InvalidOperationException("HttpContext==null, !CircuitExists");
173-
}
174-
CurrentPrincipal = principal;
175-
}
176-
}
177-
178-
private void SetHostPrincipal(Task<AuthenticationState> task)
179-
{
180-
if (AuthenticationStateProvider is IHostEnvironmentAuthenticationStateProvider hostProvider)
181-
hostProvider.SetAuthenticationState(task);
182-
}
136+
public virtual void SetUser(IPrincipal principal) => throw new NotSupportedException(nameof(SetUser));
183137

184138
/// <summary>
185139
/// Gets the local context.

Source/Csla.AspNetCore/Blazor/ApplicationContextManagerInMemory.cs

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//-----------------------------------------------------------------------
99
using Csla.Core;
1010
using Microsoft.AspNetCore.Components.Authorization;
11+
using Microsoft.AspNetCore.Http;
1112
using System.Security.Claims;
1213
using System.Security.Principal;
1314

@@ -40,14 +41,18 @@ public class ApplicationContextManagerInMemory : IContextManager, IDisposable
4041
/// </summary>
4142
protected ActiveCircuitState ActiveCircuitState { get; }
4243

44+
private readonly HttpContext HttpContext;
45+
4346
/// <summary>
4447
/// Creates an instance of the object, initializing it
4548
/// with the required IServiceProvider.
4649
/// </summary>
50+
/// <param name="httpContextAccessor"></param>
4751
/// <param name="authenticationStateProvider">AuthenticationStateProvider service</param>
4852
/// <param name="activeCircuitState"></param>
49-
public ApplicationContextManagerInMemory(AuthenticationStateProvider authenticationStateProvider, ActiveCircuitState activeCircuitState)
53+
public ApplicationContextManagerInMemory(IHttpContextAccessor httpContextAccessor, AuthenticationStateProvider authenticationStateProvider, ActiveCircuitState activeCircuitState)
5054
{
55+
HttpContext = httpContextAccessor.HttpContext;
5156
AuthenticationStateProvider = authenticationStateProvider;
5257
ActiveCircuitState = activeCircuitState;
5358
CurrentPrincipal = UnauthenticatedPrincipal;
@@ -57,29 +62,27 @@ public ApplicationContextManagerInMemory(AuthenticationStateProvider authenticat
5762

5863
private async Task InitializeUser()
5964
{
60-
Task<AuthenticationState> task = default;
61-
try
65+
var httpContext = HttpContext;
66+
if (httpContext != null)
6267
{
63-
task = AuthenticationStateProvider.GetAuthenticationStateAsync();
64-
await task;
68+
var user = httpContext.User;
69+
if (user != null)
70+
CurrentPrincipal = user;
6571
}
66-
catch (InvalidOperationException ex)
72+
else
6773
{
68-
task = Task.FromResult(new AuthenticationState(UnauthenticatedPrincipal));
69-
70-
string message = ex.Message;
71-
//see ms error https://github.com/dotnet/aspnetcore/blob/87e324a61dcd15db4086b8a8ca7bd74ca1e0a513/src/Components/Server/src/Circuits/ServerAuthenticationStateProvider.cs#L16
72-
//not much safe to test on except the error type and the use of this method name in message.
73-
if (message.Contains(nameof(AuthenticationStateProvider.GetAuthenticationStateAsync)))
74+
Task<AuthenticationState> task;
75+
try
7476
{
75-
SetHostPrincipal(task);
77+
task = AuthenticationStateProvider.GetAuthenticationStateAsync();
78+
await task;
7679
}
77-
else
80+
catch (InvalidOperationException)
7881
{
79-
throw;
82+
task = Task.FromResult(new AuthenticationState(UnauthenticatedPrincipal));
8083
}
84+
AuthenticationStateProvider_AuthenticationStateChanged(task);
8185
}
82-
AuthenticationStateProvider_AuthenticationStateChanged(task);
8386
}
8487

8588
private void AuthenticationStateProvider_AuthenticationStateChanged(Task<AuthenticationState> task)
@@ -125,31 +128,10 @@ public IPrincipal GetUser()
125128
}
126129

127130
/// <summary>
128-
/// Attempts to set the current principal on the registered
129-
/// IHostEnvironmentAuthenticationStateProvider service.
131+
/// Not supported in Blazor.
130132
/// </summary>
131133
/// <param name="principal">Principal object.</param>
132-
public virtual void SetUser(IPrincipal principal)
133-
{
134-
if (!ReferenceEquals(CurrentPrincipal, principal))
135-
{
136-
if (principal is ClaimsPrincipal claimsPrincipal)
137-
{
138-
CurrentPrincipal = principal;
139-
SetHostPrincipal(Task.FromResult(new AuthenticationState(claimsPrincipal)));
140-
}
141-
else
142-
{
143-
throw new ArgumentException("typeof(principal) != ClaimsPrincipal");
144-
}
145-
}
146-
}
147-
148-
private void SetHostPrincipal(Task<AuthenticationState> task)
149-
{
150-
if (AuthenticationStateProvider is IHostEnvironmentAuthenticationStateProvider hostProvider)
151-
hostProvider.SetAuthenticationState(task);
152-
}
134+
public virtual void SetUser(IPrincipal principal) => throw new NotSupportedException(nameof(SetUser));
153135

154136
/// <summary>
155137
/// Gets the local context.

0 commit comments

Comments
 (0)