Merge pull request 'Ensured claims injector is more exhaustive' (#34) from claims into master

Reviewed-on: #34
This commit was merged in pull request #34.
This commit is contained in:
2026-09-13 11:03:12 +02:00
2 changed files with 24 additions and 16 deletions
@@ -2,6 +2,8 @@
using Duende.IdentityServer.Services; using Duende.IdentityServer.Services;
using LiteCharmsSecurity.Admin.EntityFramework.Shared.Entities.Identity; using LiteCharmsSecurity.Admin.EntityFramework.Shared.Entities.Identity;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims; using System.Security.Claims;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -16,15 +18,30 @@ public sealed class ClaimsInjectorProfileService(UserManager<UserIdentity> userM
public async Task GetProfileDataAsync(ProfileDataRequestContext context, CancellationToken ct) public async Task GetProfileDataAsync(ProfileDataRequestContext context, CancellationToken ct)
{ {
var user = await userManager.GetUserAsync(context.Subject); var user = await userManager.GetUserAsync(context.Subject);
if (user != null && !string.IsNullOrEmpty(user.Email)) if (user != null && !string.IsNullOrEmpty(user.Email))
{ {
context.IssuedClaims.Add(new Claim("email", user.Email)); var claims = new List<Claim>
context.IssuedClaims.Add(new Claim("email_verified", user.EmailConfirmed ? "true" : "false")); {
new("email", user.Email),
new("email_verified", user.EmailConfirmed ? "true" : "false")
};
if (!string.IsNullOrEmpty(user.UserName)) if (!string.IsNullOrEmpty(user.UserName))
{ claims.Add(new Claim("preferred_username", user.UserName));
context.IssuedClaims.Add(new Claim("preferred_username", user.UserName));
} var roles = await userManager.GetRolesAsync(user);
foreach (var role in roles)
claims.Add(new Claim("role", role));
var userClaims = await userManager.GetClaimsAsync(user);
foreach (var claim in userClaims)
if (!claims.Any(c => c.Type == claim.Type && c.Value == claim.Value))
claims.Add(claim);
context.IssuedClaims = claims;
} }
} }
@@ -1,23 +1,14 @@
using System;
using System.Linq;
using Duende.IdentityServer.Hosting.DynamicProviders; using Duende.IdentityServer.Hosting.DynamicProviders;
using Duende.IdentityServer.Models; using Duende.IdentityServer.Models;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using LiteCharmsSecurity.STS.Identity.Helpers;
using System.Security.Claims;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
namespace LiteCharmsSecurity.STS.Identity.Services; namespace LiteCharmsSecurity.STS.Identity.Services;
public class OpenIdClaimsMappingConfig : ConfigureAuthenticationOptions<OpenIdConnectOptions, OidcProvider> public class OpenIdClaimsMappingConfig(IHttpContextAccessor httpContextAccessor, ILogger<ConfigureAuthenticationOptions<OpenIdConnectOptions,
OidcProvider>> logger) : ConfigureAuthenticationOptions<OpenIdConnectOptions, OidcProvider>(httpContextAccessor, logger)
{ {
public OpenIdClaimsMappingConfig(IHttpContextAccessor httpContextAccessor, ILogger<ConfigureAuthenticationOptions<OpenIdConnectOptions, OidcProvider>> logger) : base(httpContextAccessor, logger)
{
}
protected override void Configure(ConfigureAuthenticationContext<OpenIdConnectOptions, OidcProvider> context) protected override void Configure(ConfigureAuthenticationContext<OpenIdConnectOptions, OidcProvider> context)
{ {
var oidcProvider = context.IdentityProvider; var oidcProvider = context.IdentityProvider;