31 lines
898 B
C#
31 lines
898 B
C#
using System.Security.Claims;
|
|
|
|
namespace Shogi.Api.Extensions;
|
|
|
|
public static class Extensions
|
|
{
|
|
private static readonly string MsalUsernameClaim = "preferred_username";
|
|
|
|
public static string? GetGuestUserId(this ClaimsPrincipal self)
|
|
{
|
|
return self.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
|
|
}
|
|
|
|
public static string? DisplayName(this ClaimsPrincipal self)
|
|
{
|
|
return self.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
|
|
}
|
|
|
|
public static bool IsMicrosoft(this ClaimsPrincipal self)
|
|
{
|
|
return self.HasClaim(c => c.Type == MsalUsernameClaim);
|
|
}
|
|
|
|
public static string? GetMicrosoftUserId(this ClaimsPrincipal self)
|
|
{
|
|
return self.Claims.FirstOrDefault(c => c.Type == MsalUsernameClaim)?.Value;
|
|
}
|
|
|
|
public static string? GetShogiUserId(this ClaimsPrincipal self) => self.IsMicrosoft() ? self.GetMicrosoftUserId() : self.GetGuestUserId();
|
|
}
|