Skip to content

[Blazor][Wasm] Customizing the authenticated user #17650

@javiercn

Description

@javiercn

[EDIT by guardrex to add topic metadata]

Content

Steps for customizing the user bound to the application.

  • Create a class YourAccount that extends the Account class.
    public class YourAccount : Account
    {
        [JsonPropertyName("amr")]
        public string[] AuthenticationMethod { get; set; }
    }
  • Create a class YourUserFactory that extends AccountClaimsPrincipalFactory<YourAccount>
    public class YourAccountFactory : AccountClaimsPrincipalFactory<OidcAccount>
    {
        public AccountClaimsPrincipalFactory(NavigationManager navigationManager, IAccessTokenProviderAccessor accessor)
            : base(accessor)
        {
        }
    
        public async override ValueTask<ClaimsPrincipal> CreateUserAsync(
            OidcAccount account,
            RemoteAuthenticationUserOptions options)
        {
            var initialUser = await base.CreateUserAsync(account, options);
    
            if (initialUser.Identity.IsAuthenticated)
            {
                foreach (var value in account.AuthenticationMethod)
                {
                    ((ClaimsIdentity)initialUser.Identity).AddClaim(new Claim("amr", value));
                }
           }
       }
    }
  • Call
    services.AddApiAuthorization<RemoteAuthenticationState, YourAccount>()
        .AddUserFactory<RemoteAuthenticationState, YourAccount, YourUserFactory>();

Document details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions