Skip to content

Instantly share code, notes, and snippets.

@ahelland
Created December 11, 2018 07:32
Show Gist options
  • Save ahelland/ba6cd4d22d40be5ebd01bbad4aa5c7fe to your computer and use it in GitHub Desktop.
Save ahelland/ba6cd4d22d40be5ebd01bbad4aa5c7fe to your computer and use it in GitHub Desktop.
Returning claims from a token after authorization
[HttpGet]
[Authorize(Policy = "Certificate")]
[Route("Validate")]
public ActionResult<IEnumerable<string>> Validate()
{
var token = "{";
foreach (var claim in User.Claims)
{
//Datetimes are already escaped
if (claim.Type.ToString().Contains("time"))
{
token += $"\"{claim.Type}\":{claim.Value},";
}
else
{
//Let's not care about the authentication method here
//since that requires an array for building valid json
if (claim.Type == ClaimTypes.AuthenticationMethod)
{ }
else
{
token += $"\"{claim.Type}\":\"{claim.Value}\",";
}
}
}
//Remove the surplus comma
token = token.Substring(0, token.Length - 1);
token += "}";
return Content(token, "application/json");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment