mirror of
https://github.com/PacktPublishing/Web-Development-with-Blazor-Second-Edition.git
synced 2026-04-07 15:23:56 +00:00
Initial commit
This commit is contained in:
parent
2190113c56
commit
3088165398
1765 changed files with 192085 additions and 0 deletions
|
|
@ -0,0 +1,39 @@
|
|||
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Authentication.Internal;
|
||||
using System.Security.Claims;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace BlazorWebAssembly.Client;
|
||||
|
||||
public class ArrayClaimsPrincipalFactory<TAccount> : AccountClaimsPrincipalFactory<TAccount> where TAccount : RemoteUserAccount
|
||||
{
|
||||
public ArrayClaimsPrincipalFactory(IAccessTokenProviderAccessor accessor)
|
||||
: base(accessor)
|
||||
{ }
|
||||
|
||||
public async override ValueTask<ClaimsPrincipal> CreateUserAsync(TAccount account, RemoteAuthenticationUserOptions options)
|
||||
{
|
||||
var user = await base.CreateUserAsync(account, options);
|
||||
var claimsIdentity = (ClaimsIdentity?)user.Identity;
|
||||
|
||||
if (account != null)
|
||||
{
|
||||
foreach (var kvp in account.AdditionalProperties)
|
||||
{
|
||||
var name = kvp.Key;
|
||||
var value = kvp.Value;
|
||||
if (value != null && (value is JsonElement element && element.ValueKind == JsonValueKind.Array))
|
||||
{
|
||||
claimsIdentity?.RemoveClaim(claimsIdentity.FindFirst(kvp.Key));
|
||||
|
||||
var claims = element.EnumerateArray()
|
||||
.Select(x => new Claim(kvp.Key, x.ToString()));
|
||||
|
||||
claimsIdentity?.AddClaims(claims);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue