Last active
March 19, 2021 15:14
-
-
Save DhyanRathore/a201f3a5b02ea182a1a2eec6c8f835fb to your computer and use it in GitHub Desktop.
Get ClaimsPrincipal from the Request Context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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