Displaying claims in an .NET Core MVC application to Azure AD
@{ | |
ViewBag.Title = "User Claims"; | |
} | |
<h2>Welcome: @ViewBag.Name</h2> | |
<h3>Values from Identity</h3> | |
<table> | |
<tr> | |
<th> | |
IsAuthenticated - | |
</th> | |
<td> | |
@ViewBag.isAuthenticated | |
</td> | |
</tr> | |
<tr> | |
<th> | |
Name - | |
</th> | |
<td> | |
@ViewBag.Name | |
</td> | |
</tr> | |
</table> | |
<h3>Claims from ClaimsIdentity</h3> | |
<table class="table table-bordered table-striped"> | |
<tr> | |
<th> | |
Claim Type | |
</th> | |
<th> | |
Claim Value | |
</th> | |
</tr> | |
@foreach (System.Security.Claims.Claim claim in ViewBag.ClaimsIdentity) | |
{ | |
<tr> | |
<td> | |
@claim.Type | |
</td> | |
<td> | |
@claim.Value | |
</td> | |
</tr> | |
} | |
</table> |
public IActionResult Contact() | |
{ | |
ViewData["Message"] = "Your contact page."; | |
// Old way | |
//ViewBag.ClaimsIdentity = Thread.CurrentPrincipal.Identity; | |
ClaimsPrincipal user = HttpContext.User; | |
ViewBag.isAuthenticated = user.Identity.IsAuthenticated; | |
ViewBag.Name = user.Identity.Name; | |
ViewBag.ClaimsIdentity = user.Claims; | |
// Example of getting clain | |
// var email = user.FindFirst("email"); | |
return View(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
ASP.NET version:
https://gist.github.com/nzpcmad/ce16b458da97f0f08c1c1f8233f36acf