Files
monorepo/LCU.API.Collections.RepoApi-old/LCU.API.Collections.RepoApi/Controllers/RepoControllerBase.cs
T
2026-04-20 14:47:24 -05:00

38 lines
1.4 KiB
C#

using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Claims;
using LCU.API.Collections.RepoApi.Configuration;
using LCU.API.Collections.RepoApi.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.FeatureManagement.Mvc;
namespace LCU.API.Collections.RepoApi.Controllers
{
public class RepoControllerBase : ControllerBase
{
protected readonly IHttpContextAccessor HttpContextAccessor;
protected RepoControllerBase(IHttpContextAccessor httpContextAccessor)
{
HttpContextAccessor = httpContextAccessor;
}
protected AppUser CreateUserFromAuth()
{
var userClaimsPrincipal = HttpContextAccessor.HttpContext.User;
return new AppUser
{
FirstName = userClaimsPrincipal.Claims.FirstOrDefault(claim => claim.Type == ClaimTypes.GivenName)?.Value,
LastName = userClaimsPrincipal.Claims.FirstOrDefault(claim => claim.Type == ClaimTypes.Surname)?.Value,
Email = userClaimsPrincipal.Claims.FirstOrDefault(claim => claim.Type == ClaimTypes.Upn)?.Value,
Roles = userClaimsPrincipal.Claims.Where((claim) => claim.Type == ClaimTypes.Role).Select((claim) => claim?.Value).ToArray(),
};
}
public static string GetInvokerAsyncMethodName([CallerMemberName] string methodName = null) => methodName;
}
}