I have added more custom fields to the ApplicationUser class from Asp.Net Identity. I've needed fields like user's full name, location and etc.
Now I need to access those parameters in some of the views. For example to get the UserName I can simply get it with User.Identity.GetUserName()
.
How do I access the FullName, Location and other properties from the ApplicationUser class inside the views?
Have you consider using Claim ? Claim will automatically loaded to Context Identity when the user sign in.
Add claim when user is created
await userManager.AddClaimAsync(user.Id, new Claim("FullName", user.FullName));
Create extension to Identity
namespace MyApps.Extension
{
public static class IdentityExtension
{
public static string GetFullName(this IIdentity identity)
{
if (identity == null)
return null;
var fullName = (identity as ClaimsIdentity).FirstOrNull("FullName");
return fullName;
}
internal static string FirstOrNull(this ClaimsIdentity identity, string claimType)
{
var val = identity.FindFirst(claimType);
return val == null ? null : val.Value;
}
}
}
Later use
httpContextAccessor.HttpContext.User.Identity.GetFullName()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.