Last post Oct 18, 2014 10:01 AM by BrockAllen
Oct 17, 2014 07:52 PM|Jacques.Bronkhorst|LINK
I have the standard ASP Identity provider which I have added an extra property to.
My Property is FriendlyName which is the screen name that I wish to display on my View. Is there a way for me to create a standard Get method which i can use in my views for e.g. User.Identity.GetUserName() I would like to use: User.Identity.GetFriendlyName()?
I know more or less what the EF query would look like
var CurrentUserId = User.Identity.GetUserId();
var FriendlyName = new ApplicationDbContext().AspNetUser.Where(z => z.Id == CurrentUserId).Select(x => x.FriendlyName);
What can I do to add this?
Oct 17, 2014 08:52 PM|BrockAllen|LINK
Why do you want to make a round trip to the DB to get the user's name? It can be cached in the authentication cookie and then accessed as a claim on the ClaimsPrincipal via the User object.
Oct 18, 2014 02:57 AM|Jacques.Bronkhorst|LINK
Thanks for the suggestion, I have added the Claim FriendlyName.
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
await manager.AddClaimAsync(this.Id, new Claim("FriendlyName", this.FriendlyName)); //Added Claim here
How would I cache and Display it?
Oct 18, 2014 10:01 AM|BrockAllen|LINK
In your ASP.NET code:
var cp = (ClaimsPrincipal)User;
var nameClaim = cp.FindFirst(ClaimTypes.Name);
var name = nameClaim.Value;