Skip to content

Instantly share code, notes, and snippets.

@DhyanRathore
Last active March 19, 2021 15:14
Show Gist options
  • Save DhyanRathore/a201f3a5b02ea182a1a2eec6c8f835fb to your computer and use it in GitHub Desktop.
Save DhyanRathore/a201f3a5b02ea182a1a2eec6c8f835fb to your computer and use it in GitHub Desktop.
Get ClaimsPrincipal from the Request Context
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.Security.Claims;
// DEMO: Get ClaimsPrincipal from the request context
namespace ClaimsDemo.Function
{
public static class ClaimsDemo
{
[FunctionName("ClaimsDemo")]
public static IActionResult Run
([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]
HttpRequest req, ILogger log)
{
ClaimsPrincipal claimIdentity = req.HttpContext.User;
log.LogInformation("User ID: " + claimIdentity.Identity.Name);
log.LogInformation("Claim Type : Claim Value");
foreach (Claim claim in claimIdentity.Claims)
{
log.LogInformation(claim.Type + " : " + claim.Value + "\n");
}
return new OkObjectResult("Success");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment