U2 identity-mail-flows (backend): taal per gebruiker, uitnodigingsmail, opnieuw versturen, meldingen, stamp-controle

Kolommen Users.PreferredLanguage en Invitations.Language (additieve migratie). IdentityMailService
verstuurt uitnodiging, nieuwe link en meldingen bij deactiveren, heractiveren en rolwijziging;
een mailfout laat nooit een actie falen, het resultaat gaat mee in de respons naast de
kopieerbare link. Nieuwe actie resend-invite. De security stamp zit in het access token en wordt
vergeleken in de actief-controle: een wachtwoordwijziging beeindigt eerder uitgegeven access tokens,
de refresh geeft stil een nieuw token.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 19:10:44 +01:00
co-authored by Claude Sonnet 5.5
parent 2a0561a58e
commit 149f81a479
23 changed files with 1491 additions and 37 deletions
@@ -19,6 +19,9 @@ namespace SlpModularCms.Core.Identity.Authorization;
/// </remarks>
public static class ActiveUserTokenValidation
{
/// <summary>Claim carrying the user's security stamp at the moment the access token was issued.</summary>
public const string StampClaim = "stamp";
/// <summary>
/// The one place the bearer scheme is configured: signature/lifetime parameters plus the
/// active-account check. Hosts and tests both go through this, so the check cannot be dropped
@@ -46,16 +49,26 @@ public static class ActiveUserTokenValidation
}
var db = context.HttpContext.RequestServices.GetRequiredService<ApplicationDbContext>();
var isActive = await db.Users
var user = await db.Users
.AsNoTracking()
.Where(u => u.Id == userId)
.Select(u => (bool?)u.IsActive)
.Select(u => new { u.IsActive, u.SecurityStamp })
.FirstOrDefaultAsync(context.HttpContext.RequestAborted);
if (isActive != true)
if (user is null || !user.IsActive)
{
// Same 401 whether the user is gone or deactivated: which one is not the caller's business.
context.Fail("Account is not active.");
return;
}
// A password change or reset changes the stamp, which ends every access token issued before it.
// A token without the claim (issued before this check existed) is refused once; the refresh
// token then silently issues a new one that carries it.
var tokenStamp = principal?.FindFirstValue(StampClaim);
if (tokenStamp is null || !string.Equals(tokenStamp, user.SecurityStamp ?? string.Empty, StringComparison.Ordinal))
{
context.Fail("Session is no longer valid.");
}
}
}
@@ -24,6 +24,11 @@ public class ApplicationUser : IdentityUser<Guid>
/// </summary>
public string? DisplayName { get; set; }
/// <summary>
/// Voorkeurstaal van de gebruiker (nl of en), ook voor e-mails. Null = standaardtaal van de site.
/// </summary>
public string? PreferredLanguage { get; set; }
/// <summary>
/// Navigatie-eigenschap naar module-specifieke rechten.
/// </summary>
@@ -12,6 +12,10 @@ public class Invitation
public string Email { get; set; } = string.Empty;
public string Role { get; set; } = string.Empty;
public string Token { get; set; } = string.Empty;
/// <summary>Taal van de uitnodigingsmail en voorkeurstaal van het nieuwe account (nl of en); null = sitestandaard.</summary>
public string? Language { get; set; }
public DateTimeOffset ExpiryDate { get; set; }
public bool IsAccepted { get; set; }
public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
@@ -4,13 +4,13 @@ namespace SlpModularCms.Core.Identity.Models;
[ExcludeFromCodeCoverage] public record CreateOwnerRequest(string Name, string Email, string Password);
[ExcludeFromCodeCoverage] public record LoginRequest(string Email, string Password);
[ExcludeFromCodeCoverage] public record InviteUserRequest(string Email, string Role);
[ExcludeFromCodeCoverage] public record InviteUserRequest(string Email, string Role, string? Language = null);
[ExcludeFromCodeCoverage] public record CompleteSetupRequest(string Token, string Password, string? Name = null);
[ExcludeFromCodeCoverage] public record ChangeRoleRequest(string NewRole);
[ExcludeFromCodeCoverage] public record UpdateProfileRequest(string Name, string Email);
[ExcludeFromCodeCoverage] public record UpdateProfileRequest(string Name, string Email, string? Language = null);
[ExcludeFromCodeCoverage] public record ChangePasswordRequest(string CurrentPassword, string NewPassword);
[ExcludeFromCodeCoverage] public record SetUserActiveRequest(bool IsActive);
[ExcludeFromCodeCoverage] public record PendingInvitationInfo(Guid Id, string Email, string Role, string Token, DateTimeOffset CreatedAt, bool IsExpired = false);
[ExcludeFromCodeCoverage] public record PendingInvitationInfo(Guid Id, string Email, string Role, string Token, DateTimeOffset CreatedAt, bool IsExpired = false, string? Language = null);
[ExcludeFromCodeCoverage]
public record UserDto(
Guid Id,
@@ -21,4 +21,5 @@ public record UserDto(
DateTimeOffset CreatedAt,
bool InvitationPending,
string? InviteLink,
bool InvitationExpired = false);
bool InvitationExpired = false,
string? Language = null);
@@ -23,5 +23,6 @@ public record UserResponse(
string Email,
string Name,
string Role,
bool IsActive
bool IsActive,
string? Language = null
);
@@ -9,6 +9,7 @@ using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using SlpModularCms.Core.Data;
using SlpModularCms.Core.Exceptions;
using SlpModularCms.Core.Identity.Authorization;
using SlpModularCms.Core.Identity.Entities;
using SlpModularCms.Core.Identity.Models;
using SlpModularCms.Core.Observability;
@@ -117,7 +118,10 @@ public class AuthService : IAuthService
{
new(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
new(JwtRegisteredClaimNames.Email, user.Email!),
new(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
new(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
// The security stamp changes on a password change/reset. The access-token check compares it, so
// such a change ends every access token issued before it (see ActiveUserTokenValidation).
new(ActiveUserTokenValidation.StampClaim, user.SecurityStamp ?? string.Empty)
};
foreach (var role in roles)
@@ -155,7 +159,8 @@ public class AuthService : IAuthService
Email: user.Email!,
Name: user.DisplayName ?? user.Email!,
Role: primaryRole,
IsActive: user.IsActive
IsActive: user.IsActive,
Language: user.PreferredLanguage
);
return new TokenResponse(accessToken, refreshToken, expiry, userResponse);
@@ -10,7 +10,7 @@ public interface IInvitationService
/// <summary>
/// Maakt een nieuwe uitnodiging aan en retourneert het token.
/// </summary>
Task<string> CreateInvitationAsync(string email, string role);
Task<string> CreateInvitationAsync(string email, string role, string? language = null);
/// <summary>
/// Valideert of een uitnodigings-token nog geldig is.
@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using SlpModularCms.Core.Data;
using SlpModularCms.Core.Email;
using SlpModularCms.Core.Exceptions;
using SlpModularCms.Core.Identity.Entities;
using SlpModularCms.Core.Identity.Models;
@@ -25,7 +26,7 @@ public class InvitationService : IInvitationService
_settings = settings.Value;
}
public async Task<string> CreateInvitationAsync(string email, string role)
public async Task<string> CreateInvitationAsync(string email, string role, string? language = null)
{
var user = await _userManager.FindByEmailAsync(email);
if (user != null)
@@ -57,6 +58,7 @@ public class InvitationService : IInvitationService
{
Email = email,
Role = role,
Language = MailLanguages.Normalize(language),
Token = token,
ExpiryDate = DateTimeOffset.UtcNow.AddHours(_settings.ExpiryHours),
IsAccepted = false
@@ -87,6 +89,7 @@ public class InvitationService : IInvitationService
UserName = invitation.Email,
Email = invitation.Email,
DisplayName = string.IsNullOrWhiteSpace(name) ? null : name.Trim(),
PreferredLanguage = invitation.Language,
IsActive = true
};
@@ -131,7 +134,7 @@ public class InvitationService : IInvitationService
// Expired ones are included on purpose: dropping them made an invited person who did not
// activate in time vanish from the list, so the administrator could neither see nor remove them.
return invitations
.Select(i => new PendingInvitationInfo(i.Id, i.Email, i.Role, i.Token, i.CreatedAt, i.IsExpired))
.Select(i => new PendingInvitationInfo(i.Id, i.Email, i.Role, i.Token, i.CreatedAt, i.IsExpired, i.Language))
.ToList();
}