Files
jdescopingtool/OLD/WebInterface/Security/UserIdentity.cs
T
Joseph Doherty 26ff8d9b4f 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.
2026-01-02 07:43:29 -05:00

80 lines
3.1 KiB
C#
Executable File

using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using DataModel.Models;
using Microsoft.AspNet.Identity;
namespace WebInterface.Security
{
/// <summary>
/// Custom user identity for tracking user-related data
/// </summary>
public sealed class UserIdentity : ClaimsIdentity
{
/// <summary>
/// Constuctor
/// </summary>
/// <param name="claims">Claims for identity</param>
/// <param name="authenticationType">Type of authentication for identity</param>
public UserIdentity(IEnumerable<Claim> claims, string authenticationType = DefaultAuthenticationTypes.ApplicationCookie)
: base(claims, authenticationType)
{
}
/// <summary>
/// Constuctor
/// </summary>
/// <param name="claimsIdentity">Identity to parse from</param>
public UserIdentity(ClaimsIdentity claimsIdentity)
: base(claimsIdentity.Claims, claimsIdentity.AuthenticationType)
{
}
/// <summary>
/// Extracts the keyed claims to generate LDAPEntry for user
/// </summary>
/// <returns>LDAPEntry for current user</returns>
public LDAPEntry ToLDAPEntry()
{
return new LDAPEntry()
{
DN = GetClaimValue(ClaimTypes.NameIdentifier),
Username = GetClaimValue(ClaimTypes.WindowsAccountName),
FirstName = GetClaimValue(ClaimTypes.GivenName),
LastName = GetClaimValue(ClaimTypes.Surname),
Title = GetClaimValue(ClaimTypes.Role),
EmailAddress = GetClaimValue(ClaimTypes.Email)
};
}
/// <summary>
/// Gets the value assigned to the designated claim
/// </summary>
/// <param name="claimID">Type/ID of claim to get value for</param>
/// <returns>Value of designed claim</returns>
private string GetClaimValue(string claimID)
{
Claim claim = Claims.FirstOrDefault(c => c.Type == claimID);
return claim == null ? string.Empty : claim.Value;
}
/// <summary>
/// Generates user identify from LDAP entry
/// </summary>
/// <param name="ldapEntry">LDAP entry to get values for claims</param>
/// <returns>User identity with claims for LDAP entry values</returns>
public static UserIdentity FromLDAPEntry(LDAPEntry ldapEntry)
{
//Create claims from LDAP entry values
List<Claim> claims = new List<Claim> {
new Claim(ClaimTypes.NameIdentifier, ldapEntry.DN),
new Claim(ClaimTypes.WindowsAccountName, ldapEntry.Username),
new Claim(ClaimTypes.GivenName, ldapEntry.FirstName),
new Claim(ClaimTypes.Surname, ldapEntry.LastName),
new Claim(ClaimTypes.Role, ldapEntry.Title),
new Claim(ClaimTypes.Email, ldapEntry.EmailAddress)
};
return new UserIdentity(claims);
}
}
}