Skip to content

Instantly share code, notes, and snippets.

@jeffward01
Forked from imeanitworks/IdentityHelper.cs
Created October 11, 2022 20:24
Show Gist options
  • Save jeffward01/5cf75fb3f5c994ae76273c84e22db706 to your computer and use it in GitHub Desktop.
Save jeffward01/5cf75fb3f5c994ae76273c84e22db706 to your computer and use it in GitHub Desktop.
Helper class that I use to extract claim information from the authentication context
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading;
namespace ProjectNamespace
{
public static class IdentityHelper
{
public class Profile
{
public string Email { get; set; }
public string DisplayName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string NtName { get; set; }
}
private const RoleKey = "role";
private const FirstNameKey = "given_name";
private const LastNameKey = "family_name";
private const NtNameKey = "nt_name";
private const EmailKey = "email"
private const DisplayNameKey = "name";
private const SubjectKey = "sub";
private static ClaimsIdentity User { get; set; }
private static ClaimsIdentity Get()
{
try
{
return (Thread.CurrentPrincipal.Identity as ClaimsIdentity);
}
catch(Exception e)
{
throw new InvalidOperationException("Unable to cast the authenticated user to ClaimsIdentity");
}
}
private static void SetUser()
{
User = Get();
}
public static List<string> GetRoles()
{
if (User != null)
{
try
{
return User.FindAll(RoleKey).ToList().ConvertAll(x => x.Value);
}
catch (Exception e)
{
throw new ArgumentOutOfRangeException("Unable to find user roles in claims identity for authenticated user.");
}
}
return null;
}
public static Profile GetProfile()
{
SetUser();
try
{
return new Profile
{
NtName = GetUserNtName(),
LastName = GetLastName(),
FirstName = GetFirstName(),
Email = GetEmail(),
DisplayName = GetUserDisplayName()
};
}
catch(Exception e)
{
throw new InvalidOperationException($"Encountered issue while getting user profile from token: {e.Message}");
}
}
private static string GetUserNtName()
{
if(User != null)
{
try
{
return User.FindFirst(NtNameKey).Value;
}
catch (Exception e)
{
throw new ArgumentOutOfRangeException("Unable to find 'nt_name' in claims identity for authenticated user.");
}
}
return null;
}
private static string GetEmail()
{
if(User != null)
{
try
{
return User.FindFirst(EmailKey).Value;
}
catch(Exception e)
{
throw new ArgumentOutOfRangeException("Unable to find 'email' in claims identity for authenticated user.");
}
}
return null;
}
private static string GetFirstName()
{
if(User != null)
{
try
{
return User.FindFirst(FirstNameKey).Value;
}
catch(Exception e)
{
throw new ArgumentOutOfRangeException("Unable to find 'given_name' in claims identity for authenticated user.");
}
}
return null;
}
private static string GetLastName()
{
if (User != null)
{
try
{
return User.FindFirst(LastNameKey).Value;
}
catch (Exception e)
{
throw new ArgumentOutOfRangeException("Unable to find 'family_name' in claims identity for authenticated user.");
}
}
return null;
}
public static string GetUserDisplayName()
{
if (User != null)
{
try
{
return User.FindFirst(DisplayNameKey).Value;
}
catch (Exception e)
{
throw new ArgumentOutOfRangeException("Unable to find 'name' in claims identity for authenticated user.");
}
}
return null;
}
public static string GetUserGuid()
{
if(User != null)
{
try
{
return User.FindFirst(SubjectKey).Value;
}
catch(Exception e)
{
}
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment