using System.Security.Claims; // Create a ClaimsPrincipal object var identity = new ClaimsIdentity(new[] { new Claim("name", "Alice"), new Claim("email", "[email protected]"), }); var principal = new ClaimsPrincipal(identity); // Find the email claim value var email = principal.FindFirstValue("email"); Console.WriteLine("Email: " + email);
using System.Security.Claims; // Find the current user's email claim value var principal = HttpContext.User as ClaimsPrincipal; var email = principal?.FindFirstValue("email"); Console.WriteLine("Email: " + email);This example retrieves the current user's ClaimsPrincipal object from the HttpContext and uses the FindFirstValue method to retrieve the email claim value. These examples use the System.Security.Claims namespace, which is part of the .NET Core library.