(C# ASP.NET Core) Displaying User Info on each Page he visits

This is a snippet on showing "You are logged as ..." on every page that an authenticated and authorized user visits. The code can be inserted in a razor page or in a partial page.

Categories | About |     |  

Parveen,

Code Snippet

Remember that: @User is available on every razor page, and on every partial page

The information saved in Claim collection can be queried as follows

// namespace 
using System.Security.Claims;

// assuming EMail was saved in ClaimsIdentity 
// during SignIn 
String userEMail = User.FindFirst(ClaimTypes.Email).Value;

// assuming Name was saved in ClaimsIdentity 
// during SignIn 
String userName= User.FindFirst(ClaimTypes.Name).Value;

Following is an example of a snippet for displaying the info on a razor page or a partial page.


// typically this snippet can be written inside a partial page 
// for header or sidebar 

<span class="navbar-brand col-md-3 col-lg-2 me-0 px-3">

   Welcome @User.FindFirst(System.Security.Claims.ClaimTypes.Email).Value !

</span>

Video Explanation

Please watch the following youtube video:


This Blog Post/Article "(C# ASP.NET Core) Displaying User Info on each Page he visits" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Comments and Discussion