public UserAccount(ClaimsPrincipal user)
{
if (HttpContext.Current.Request.IsAuthenticated)
{
Principal = user;
// Extract the role claims
RoleClaims = user.Claims.Where(c => c.Type == ClaimTypes.Role).Select(c => c.Value);
// User name
Name = user.Identity.Name;
// Extract the given name(s); if null or empty, then set an initial value
string gn = user.Claims.SingleOrDefault(c => c.Type == ClaimTypes.GivenName).Value;
if (string.IsNullOrEmpty(gn)) { gn = "(empty given name)"; }
GivenName = gn;
// Extract the surname; if null or empty, then set an initial value
string sn = user.Claims.SingleOrDefault(c => c.Type == ClaimTypes.Surname).Value;
if (string.IsNullOrEmpty(sn)) { sn = "(empty surname)"; }
Surname = sn;
IsAuthenticated = true;
IsAdmin = user.HasClaim(ClaimTypes.Role, "Admin") ? true : false;
}
else
{
RoleClaims = new List<string>();
Name = "anonymous";
GivenName = "Unauthenticated";
Surname = "Anonymous";
IsAuthenticated = false;
IsAdmin = false;
}
NamesFirstLast = $"{GivenName} {Surname}";
NamesLastFirst = $"{Surname}, {GivenName}";
}