im using MVC 5 and i found this : User.Identity.Name full name mvc5 , but i dont know how use the "Extension Method on Identity" where i put this code, and where...please help me
Extension Method on Identity:
public static class GenericPrincipalExtensions
{
public static string FullName(this IPrincipal user)
{
if (user.Identity.IsAuthenticated)
{
ClaimsIdentity claimsIdentity = user.Identity as ClaimsIdentity;
foreach (var claim in claimsIdentity.Claims)
{
if (claim.Type == "FullName")
return claim.Value;
}
return "";
}
else
return "";
}
}
using System.Security.Claims;
using System.Security.Principal;
public static class IdentityExtended
{
public static string GetFullName(this IIdentity identity)
{
IEnumerable<Claim> claims = ((ClaimsIdentity)identity).Claims;
var FullName = claims.Where(c => c.Type == "FullName").SingleOrDefault();
return FullName.Value;
}
}
Inside View for Example write as below
<h1 class="display-4 rainbow-text">Hello, @User.Identity.GetFullName()!</h1>
Just make sure you put the class inside solution itself no inside a subfolder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With