-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Make static prerendering support auth. Fixes #11799 #12318
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cff2b94
Make E2E prerendering test use static prerendering (we no longer need…
SteveSandersonMS 38accfd
Use authentication state during static prerendering. This replicates …
SteveSandersonMS a49a2b9
Initialize the authentication state provider during static prerendering
SteveSandersonMS 6409705
Update ref assembly
SteveSandersonMS 01f3394
Update unit test
SteveSandersonMS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Components/Components/src/Auth/IHostEnvironmentAuthenticationStateProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.AspNetCore.Components | ||
{ | ||
/// <summary> | ||
/// An interface implemented by <see cref="AuthenticationStateProvider"/> classes that can receive authentication | ||
/// state information from the host environment. | ||
/// </summary> | ||
public interface IHostEnvironmentAuthenticationStateProvider | ||
{ | ||
/// <summary> | ||
/// Supplies updated authentication state data to the <see cref="AuthenticationStateProvider"/>. | ||
/// </summary> | ||
/// <param name="authenticationStateTask">A task that resolves with the updated <see cref="AuthenticationState"/>.</param> | ||
void SetAuthenticationState(Task<AuthenticationState> authenticationStateTask); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
src/Components/Server/src/Circuits/FixedAuthenticationStateProvider.cs
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
src/Components/Server/src/Circuits/ServerAuthenticationStateProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.AspNetCore.Components.Server.Circuits | ||
{ | ||
/// <summary> | ||
/// An <see cref="AuthenticationStateProvider"/> intended for use in server-side Blazor. | ||
/// </summary> | ||
internal class ServerAuthenticationStateProvider : AuthenticationStateProvider, IHostEnvironmentAuthenticationStateProvider | ||
{ | ||
private Task<AuthenticationState> _authenticationStateTask; | ||
|
||
public override Task<AuthenticationState> GetAuthenticationStateAsync() | ||
=> _authenticationStateTask | ||
?? throw new InvalidOperationException($"{nameof(GetAuthenticationStateAsync)} was called before {nameof(SetAuthenticationState)}."); | ||
|
||
public void SetAuthenticationState(Task<AuthenticationState> authenticationStateTask) | ||
{ | ||
_authenticationStateTask = authenticationStateTask ?? throw new ArgumentNullException(nameof(authenticationStateTask)); | ||
NotifyAuthenticationStateChanged(_authenticationStateTask); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
src/Components/Server/test/Circuits/FixedAuthenticationStateProviderTest.cs
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
src/Components/Server/test/Circuits/ServerAuthenticationStateProviderTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Components.Server.Circuits; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Components.Server.Tests.Circuits | ||
{ | ||
public class ServerAuthenticationStateProviderTest | ||
{ | ||
[Fact] | ||
public async Task CannotProvideAuthenticationStateBeforeInitialization() | ||
{ | ||
await Assert.ThrowsAsync<InvalidOperationException>(() => | ||
new ServerAuthenticationStateProvider() | ||
.GetAuthenticationStateAsync()); | ||
} | ||
|
||
[Fact] | ||
public async Task SuppliesAuthenticationStateWithFixedUser() | ||
{ | ||
// Arrange | ||
var user = new ClaimsPrincipal(); | ||
var provider = new ServerAuthenticationStateProvider(); | ||
|
||
// Act 1 | ||
var expectedAuthenticationState1 = new AuthenticationState(user); | ||
provider.SetAuthenticationState(Task.FromResult(expectedAuthenticationState1)); | ||
|
||
// Assert 1 | ||
var actualAuthenticationState1 = await provider.GetAuthenticationStateAsync(); | ||
Assert.NotNull(actualAuthenticationState1); | ||
Assert.Same(expectedAuthenticationState1, actualAuthenticationState1); | ||
|
||
// Act 2: Show we can update it further | ||
var expectedAuthenticationState2 = new AuthenticationState(user); | ||
provider.SetAuthenticationState(Task.FromResult(expectedAuthenticationState2)); | ||
|
||
// Assert 2 | ||
var actualAuthenticationState2 = await provider.GetAuthenticationStateAsync(); | ||
Assert.NotNull(actualAuthenticationState2); | ||
Assert.NotSame(actualAuthenticationState1, actualAuthenticationState2); | ||
Assert.Same(expectedAuthenticationState2, actualAuthenticationState2); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not super sure what this is needed, but the name immediately threw me off path, its too similar to IHostingEnvironment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we instead, have an HttpContextAuthenticationStateProvider in MVC, and have the FixedAuthenticationStateProvider contain this extra method that you can call during reconnection?
That way we don't have to carry around an additional interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That doesn't work, because
StaticComponentRenderer
still has to initialize whateverAuthenticationStateProvider
you have in DI. If the one you have there is aFixedAuthenticationStateProvider
, it wouldn't be able to invoke any methods on it, because it lives in an assembly not referenced from ViewFeatures.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about adding it to the base class instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not applicable in general because, say, in client-side Blazor there's no host environment that can tell you what the authentication state is. The
AuthenticationStateProvider
concept is about providing the authentication state, not receiving it from someone else.I don't mind renaming
IHostEnvironmentAuthenticationStateProvider
to something else if you prefer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want MVC referencing signalr.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, now I remember.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is about the functionality, yes. Setting the state has no place on the base contract as it’s not a concept that exists generally (at all, not just multiple times). That’s why I’m avoiding putting it there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@javiercn Do you have any further concerns about this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I think we have a different trade-off balance, so I leave it to your better judgement.