-
Notifications
You must be signed in to change notification settings - Fork 10k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build in AuthenticationStateProviders from project templates (#55821)
- Loading branch information
Showing
43 changed files
with
661 additions
and
419 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/Components/Authorization/src/AuthenticationStateData.cs
This file contains 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,27 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Security.Claims; | ||
|
||
namespace Microsoft.AspNetCore.Components.Authorization; | ||
|
||
/// <summary> | ||
/// A JSON-serializable type that represents the data that is used to create an <see cref="AuthenticationState"/>. | ||
/// </summary> | ||
public class AuthenticationStateData | ||
{ | ||
/// <summary> | ||
/// The client-readable claims that describe the <see cref="AuthenticationState.User"/>. | ||
/// </summary> | ||
public IList<KeyValuePair<string, string>> Claims { get; set; } = []; | ||
|
||
/// <summary> | ||
/// Gets the value that identifies 'Name' claims. This is used when returning the property <see cref="ClaimsIdentity.Name"/>. | ||
/// </summary> | ||
public string NameClaimType { get; set; } = ClaimsIdentity.DefaultNameClaimType; | ||
|
||
/// <summary> | ||
/// Gets the value that identifies 'Role' claims. This is used when calling <see cref="ClaimsPrincipal.IsInRole"/>. | ||
/// </summary> | ||
public string RoleClaimType { get; set; } = ClaimsIdentity.DefaultRoleClaimType; | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1 +1,9 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Components.Authorization.AuthenticationStateData | ||
Microsoft.AspNetCore.Components.Authorization.AuthenticationStateData.AuthenticationStateData() -> void | ||
Microsoft.AspNetCore.Components.Authorization.AuthenticationStateData.Claims.get -> System.Collections.Generic.IList<System.Collections.Generic.KeyValuePair<string!, string!>>! | ||
Microsoft.AspNetCore.Components.Authorization.AuthenticationStateData.Claims.set -> void | ||
Microsoft.AspNetCore.Components.Authorization.AuthenticationStateData.NameClaimType.get -> string! | ||
Microsoft.AspNetCore.Components.Authorization.AuthenticationStateData.NameClaimType.set -> void | ||
Microsoft.AspNetCore.Components.Authorization.AuthenticationStateData.RoleClaimType.get -> string! | ||
Microsoft.AspNetCore.Components.Authorization.AuthenticationStateData.RoleClaimType.set -> void |
This file contains 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 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 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 |
---|---|---|
@@ -1,4 +1,8 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Components.Routing.RazorComponentsEndpointHttpContextExtensions | ||
Microsoft.AspNetCore.Components.Server.ServerAuthenticationStateProvider | ||
Microsoft.AspNetCore.Components.Server.ServerAuthenticationStateProvider.ServerAuthenticationStateProvider() -> void | ||
Microsoft.AspNetCore.Components.Server.ServerAuthenticationStateProvider.SetAuthenticationState(System.Threading.Tasks.Task<Microsoft.AspNetCore.Components.Authorization.AuthenticationState!>! authenticationStateTask) -> void | ||
override Microsoft.AspNetCore.Components.Server.ServerAuthenticationStateProvider.GetAuthenticationStateAsync() -> System.Threading.Tasks.Task<Microsoft.AspNetCore.Components.Authorization.AuthenticationState!>! | ||
static Microsoft.AspNetCore.Components.Endpoints.Infrastructure.ComponentEndpointConventionBuilderHelper.GetEndpointRouteBuilder(Microsoft.AspNetCore.Builder.RazorComponentsEndpointConventionBuilder! builder) -> Microsoft.AspNetCore.Routing.IEndpointRouteBuilder! | ||
static Microsoft.AspNetCore.Components.Routing.RazorComponentsEndpointHttpContextExtensions.AcceptsInteractiveRouting(this Microsoft.AspNetCore.Http.HttpContext! context) -> bool |
This file contains 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 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,6 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: TypeForwardedTo(typeof(Microsoft.AspNetCore.Components.Server.ServerAuthenticationStateProvider))] |
This file contains 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
73 changes: 73 additions & 0 deletions
73
src/Components/WebAssembly/Server/src/AuthenticationStateSerializationOptions.cs
This file contains 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,73 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Linq; | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Components.Authorization; | ||
|
||
namespace Microsoft.AspNetCore.Components.WebAssembly.Server; | ||
|
||
/// <summary> | ||
/// Provides options for configuring the JSON serialization of the <see cref="AuthenticationState"/> provided by the server's <see cref="AuthenticationStateProvider"/> | ||
/// to the WebAssembly client using <see cref="PersistentComponentState"/>. | ||
/// </summary> | ||
public class AuthenticationStateSerializationOptions | ||
{ | ||
/// <summary> | ||
/// Default constructor for <see cref="AuthenticationStateSerializationOptions"/>. | ||
/// </summary> | ||
public AuthenticationStateSerializationOptions() | ||
{ | ||
SerializationCallback = SerializeAuthenticationStateAsync; | ||
} | ||
|
||
/// <summary> | ||
/// If <see langword="true"/>, the default <see cref="SerializationCallback"/> will serialize all claims; otherwise, it will only serialize | ||
/// the <see cref="ClaimsIdentity.NameClaimType"/> and <see cref="ClaimsIdentity.RoleClaimType"/> claims. | ||
/// </summary> | ||
public bool SerializeAllClaims { get; set; } | ||
|
||
/// <summary> | ||
/// Default implementation for converting the server's <see cref="AuthenticationState"/> to an <see cref="AuthenticationStateData"/> object | ||
/// for JSON serialization to the client using <see cref="PersistentComponentState"/>."/> | ||
/// </summary> | ||
public Func<AuthenticationState, ValueTask<AuthenticationStateData?>> SerializationCallback { get; set; } | ||
|
||
private ValueTask<AuthenticationStateData?> SerializeAuthenticationStateAsync(AuthenticationState authenticationState) | ||
{ | ||
AuthenticationStateData? data = null; | ||
|
||
if (authenticationState.User.Identity?.IsAuthenticated ?? false) | ||
{ | ||
data = new AuthenticationStateData(); | ||
|
||
if (authenticationState.User.Identities.FirstOrDefault() is { } identity) | ||
{ | ||
data.NameClaimType = identity.NameClaimType; | ||
data.RoleClaimType = identity.RoleClaimType; | ||
} | ||
|
||
if (SerializeAllClaims) | ||
{ | ||
foreach (var claim in authenticationState.User.Claims) | ||
{ | ||
data.Claims.Add(new(claim.Type, claim.Value)); | ||
} | ||
} | ||
else | ||
{ | ||
if (authenticationState.User.FindFirst(data.NameClaimType) is { } nameClaim) | ||
{ | ||
data.Claims.Add(new(nameClaim.Type, nameClaim.Value)); | ||
} | ||
|
||
foreach (var roleClaim in authenticationState.User.FindAll(data.RoleClaimType)) | ||
{ | ||
data.Claims.Add(new(roleClaim.Type, roleClaim.Value)); | ||
} | ||
} | ||
} | ||
|
||
return ValueTask.FromResult(data); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/Components/WebAssembly/Server/src/AuthenticationStateSerializer.cs
This file contains 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,52 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Components.Authorization; | ||
using Microsoft.AspNetCore.Components.Web; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.AspNetCore.Components.WebAssembly.Server; | ||
|
||
internal sealed class AuthenticationStateSerializer : IHostEnvironmentAuthenticationStateProvider, IDisposable | ||
{ | ||
// Do not change. This must match all versions of the server-side DeserializedAuthenticationStateProvider.PersistenceKey. | ||
internal const string PersistenceKey = $"__internal__{nameof(AuthenticationState)}"; | ||
|
||
private readonly PersistentComponentState _state; | ||
private readonly Func<AuthenticationState, ValueTask<AuthenticationStateData?>> _serializeCallback; | ||
private readonly PersistingComponentStateSubscription _subscription; | ||
|
||
private Task<AuthenticationState>? _authenticationStateTask; | ||
|
||
public AuthenticationStateSerializer(PersistentComponentState persistentComponentState, IOptions<AuthenticationStateSerializationOptions> options) | ||
{ | ||
_state = persistentComponentState; | ||
_serializeCallback = options.Value.SerializationCallback; | ||
_subscription = persistentComponentState.RegisterOnPersisting(OnPersistingAsync, RenderMode.InteractiveWebAssembly); | ||
} | ||
|
||
private async Task OnPersistingAsync() | ||
{ | ||
if (_authenticationStateTask is null) | ||
{ | ||
throw new InvalidOperationException($"{nameof(SetAuthenticationState)} must be called before the {nameof(PersistentComponentState)}.{nameof(PersistentComponentState.RegisterOnPersisting)} callback."); | ||
} | ||
|
||
var authenticationStateData = await _serializeCallback(await _authenticationStateTask); | ||
if (authenticationStateData is not null) | ||
{ | ||
_state.PersistAsJson(PersistenceKey, authenticationStateData); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void SetAuthenticationState(Task<AuthenticationState> authenticationStateTask) | ||
{ | ||
_authenticationStateTask = authenticationStateTask ?? throw new ArgumentNullException(nameof(authenticationStateTask)); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_subscription.Dispose(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Components/WebAssembly/Server/src/PublicAPI.Unshipped.txt
This file contains 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 |
---|---|---|
@@ -1,5 +1,12 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Components.WebAssembly.Server.AuthenticationStateSerializationOptions | ||
Microsoft.AspNetCore.Components.WebAssembly.Server.AuthenticationStateSerializationOptions.AuthenticationStateSerializationOptions() -> void | ||
Microsoft.AspNetCore.Components.WebAssembly.Server.AuthenticationStateSerializationOptions.SerializationCallback.get -> System.Func<Microsoft.AspNetCore.Components.Authorization.AuthenticationState!, System.Threading.Tasks.ValueTask<Microsoft.AspNetCore.Components.Authorization.AuthenticationStateData?>>! | ||
Microsoft.AspNetCore.Components.WebAssembly.Server.AuthenticationStateSerializationOptions.SerializationCallback.set -> void | ||
Microsoft.AspNetCore.Components.WebAssembly.Server.AuthenticationStateSerializationOptions.SerializeAllClaims.get -> bool | ||
Microsoft.AspNetCore.Components.WebAssembly.Server.AuthenticationStateSerializationOptions.SerializeAllClaims.set -> void | ||
Microsoft.AspNetCore.Components.WebAssembly.Server.WebAssemblyComponentsEndpointOptions.ServeMultithreadingHeaders.get -> bool | ||
Microsoft.AspNetCore.Components.WebAssembly.Server.WebAssemblyComponentsEndpointOptions.ServeMultithreadingHeaders.set -> void | ||
Microsoft.AspNetCore.Components.WebAssembly.Server.WebAssemblyComponentsEndpointOptions.StaticAssetsManifestPath.get -> string? | ||
Microsoft.AspNetCore.Components.WebAssembly.Server.WebAssemblyComponentsEndpointOptions.StaticAssetsManifestPath.set -> void | ||
static Microsoft.Extensions.DependencyInjection.WebAssemblyRazorComponentsBuilderExtensions.AddAuthenticationStateSerialization(this Microsoft.Extensions.DependencyInjection.IRazorComponentsBuilder! builder, System.Action<Microsoft.AspNetCore.Components.WebAssembly.Server.AuthenticationStateSerializationOptions!>? configure = null) -> Microsoft.Extensions.DependencyInjection.IRazorComponentsBuilder! |
This file contains 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
Oops, something went wrong.