Initial commit: JDE Scoping Tool migration project
Set up repository with legacy .NET Framework 4.8 source (OLD/), new .NET 10 Blazor solution (NEW/), OpenSpec specifications, documentation, and project configuration.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using System.Security.Claims;
|
||||
using JdeScoping.Core.Models;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
|
||||
namespace JdeScoping.Api.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for ClaimsPrincipal
|
||||
/// </summary>
|
||||
public static class ClaimsExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a ClaimsPrincipal to a UserInfo instance
|
||||
/// </summary>
|
||||
/// <param name="principal">Claims principal to extract user info from</param>
|
||||
/// <returns>UserInfo populated from claims</returns>
|
||||
public static UserInfo? ToUserInfo(this ClaimsPrincipal principal)
|
||||
{
|
||||
if (principal.Identity?.IsAuthenticated != true)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new UserInfo
|
||||
{
|
||||
Dn = principal.FindFirstValue("dn") ?? principal.FindFirstValue(ClaimTypes.NameIdentifier) ?? string.Empty,
|
||||
Username = principal.FindFirstValue(ClaimTypes.Name) ?? string.Empty,
|
||||
FirstName = principal.FindFirstValue(ClaimTypes.GivenName) ?? string.Empty,
|
||||
LastName = principal.FindFirstValue(ClaimTypes.Surname) ?? string.Empty,
|
||||
EmailAddress = principal.FindFirstValue(ClaimTypes.Email) ?? string.Empty,
|
||||
Title = principal.FindFirstValue("title") ?? string.Empty
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a ClaimsIdentity from a UserInfo instance
|
||||
/// </summary>
|
||||
/// <param name="user">User information to create claims from</param>
|
||||
/// <returns>ClaimsIdentity with user claims</returns>
|
||||
public static ClaimsIdentity FromUserInfo(UserInfo user)
|
||||
{
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new(ClaimTypes.NameIdentifier, user.Dn),
|
||||
new(ClaimTypes.Name, user.Username),
|
||||
new(ClaimTypes.GivenName, user.FirstName),
|
||||
new(ClaimTypes.Surname, user.LastName),
|
||||
new(ClaimTypes.Email, user.EmailAddress),
|
||||
new("title", user.Title),
|
||||
new("dn", user.Dn)
|
||||
};
|
||||
|
||||
return new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user