Added custom profile claims injector

This commit is contained in:
2026-09-02 15:00:35 +02:00
parent 3be84072d3
commit b8537ff7c6
5 changed files with 59 additions and 18 deletions
+2 -2
View File
@@ -9,10 +9,10 @@ WORKDIR /app_source
COPY . .
RUN dotnet restore "LiteCharmsSecurity.AdminUI.sln" --ignore-failed-sources --source "https://nexus.khongisa.co.za/repository/nuget-group/index.json"
RUN dotnet restore "LiteCharmsSecurity.AdminUI.sln"
WORKDIR "/app_source/src/LiteCharmsSecurity.Admin.Api"
RUN dotnet build "LiteCharmsSecurity.Admin.Api.csproj" -c Release -o /app/build --source "https://nexus.khongisa.co.za/repository/nuget-group/index.json"
RUN dotnet build "LiteCharmsSecurity.Admin.Api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "LiteCharmsSecurity.Admin.Api.csproj" -c Release --no-restore -o /app/publish
+2 -2
View File
@@ -18,10 +18,10 @@ RUN ln -s /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm \
COPY . .
RUN dotnet restore "LiteCharmsSecurity.AdminUI.sln" --ignore-failed-sources --source "https://nexus.khongisa.co.za/repository/nuget-group/index.json"
RUN dotnet restore "LiteCharmsSecurity.AdminUI.sln"
WORKDIR "/app_source/src/LiteCharmsSecurity.Admin"
RUN dotnet build "LiteCharmsSecurity.Admin.csproj" -c Release -o /app/build --source "https://nexus.khongisa.co.za/repository/nuget-group/index.json"
RUN dotnet build "LiteCharmsSecurity.Admin.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "LiteCharmsSecurity.Admin.csproj" -c Release --no-restore -o /app/publish
@@ -9,10 +9,10 @@ WORKDIR /app_source
COPY . .
RUN dotnet restore "LiteCharmsSecurity.AdminUI.sln" --ignore-failed-sources --source "https://nexus.khongisa.co.za/repository/nuget-group/index.json"
RUN dotnet restore "LiteCharmsSecurity.AdminUI.sln"
WORKDIR "/app_source/src/LiteCharmsSecurity.STS.Identity"
RUN dotnet build "LiteCharmsSecurity.STS.Identity.csproj" -c Release -o /app/build --source "https://nexus.khongisa.co.za/repository/nuget-group/index.json"
RUN dotnet build "LiteCharmsSecurity.STS.Identity.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "LiteCharmsSecurity.STS.Identity.csproj" -c Release --no-restore -o /app/publish
@@ -0,0 +1,39 @@
using Duende.IdentityServer.Models;
using Duende.IdentityServer.Services;
using LiteCharmsSecurity.Admin.EntityFramework.Shared.Entities.Identity;
using Microsoft.AspNetCore.Identity;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
namespace LiteCharmsSecurity.STS.Identity.Services;
public sealed class ClaimsInjectorProfileService(UserManager<UserIdentity> userManager) : IProfileService
{
public Task GetProfileDataAsync(ProfileDataRequestContext context)
=> GetProfileDataAsync(context, CancellationToken.None);
public async Task GetProfileDataAsync(ProfileDataRequestContext context, CancellationToken ct)
{
var user = await userManager.GetUserAsync(context.Subject);
if (user != null && !string.IsNullOrEmpty(user.Email))
{
context.IssuedClaims.Add(new Claim("email", user.Email));
context.IssuedClaims.Add(new Claim("email_verified", user.EmailConfirmed ? "true" : "false"));
if (!string.IsNullOrEmpty(user.UserName))
{
context.IssuedClaims.Add(new Claim("preferred_username", user.UserName));
}
}
}
public Task IsActiveAsync(IsActiveContext context)
=> IsActiveAsync(context, CancellationToken.None);
public async Task IsActiveAsync(IsActiveContext context, CancellationToken ct)
{
var user = await userManager.GetUserAsync(context.Subject);
context.IsActive = user != null;
}
}
+14 -12
View File
@@ -1,24 +1,25 @@
using System;
using System.Threading.Tasks;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using IdentityModel;
using Duende.IdentityServer;
using Duende.IdentityServer.Configuration;
using HealthChecks.UI.Client;
using IdentityModel;
using LiteCharmsSecurity.Admin.EntityFramework.Shared.DbContexts;
using LiteCharmsSecurity.Admin.EntityFramework.Shared.Entities.Identity;
using Skoruba.Duende.IdentityServer.Shared.Configuration.Helpers;
using LiteCharmsSecurity.STS.Identity.Configuration;
using LiteCharmsSecurity.STS.Identity.Configuration.Constants;
using LiteCharmsSecurity.STS.Identity.Configuration.Interfaces;
using LiteCharmsSecurity.STS.Identity.Helpers;
using LiteCharmsSecurity.STS.Identity.Passkeys;
using LiteCharmsSecurity.STS.Identity.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Skoruba.Duende.IdentityServer.Shared.Configuration.Helpers;
using System;
using System.Threading.Tasks;
namespace LiteCharmsSecurity.STS.Identity
{
@@ -129,7 +130,8 @@ namespace LiteCharmsSecurity.STS.Identity
public virtual void RegisterAuthentication(IServiceCollection services)
{
services.AddAuthenticationServices<AdminIdentityDbContext, UserIdentity, UserIdentityRole>(Configuration);
services.AddIdentityServer<IdentityServerConfigurationDbContext, IdentityServerPersistedGrantDbContext, UserIdentity>(Configuration);
services.AddIdentityServer<IdentityServerConfigurationDbContext, IdentityServerPersistedGrantDbContext, UserIdentity>(Configuration)
.AddProfileService<ClaimsInjectorProfileService>();
}
public virtual void RegisterAuthorization(IServiceCollection services)