In the ASP.Net MVC 5, the ApplicationUser can be extended to have custom property. I have extended it such that it now has a new property called DisplayName:
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser {
public string ConfirmationToken { get; set; }
public string DisplayName { get; set; } //here it is!
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
return userIdentity;
}
}
I also have updated the database table using Update-Database command in the Package-Manager Console in Visual Studio to ensure the consistency between the ApplicationUser class and the AspNetUsers table. I have confirmed that the new column called DisplayName is now exist in the AspNetUsers table.
Now, I want to use that DisplayName instead of the default UserName for the text in the original _LoginPartial.cshtml View. But as you can see:
<ul class="nav navbar-nav navbar-right">
<li>
@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
</li>
<li><a href="javascript:document.getElementById('logoutForm').submit()" rel="nofollow noreferrer noopener">Log off</a></li>
</ul>
The original _LoginPartialView.cshtml is using User.Identity.GetUserName() to get the UserName of the ApplicationUser. The User.Identity has GetUserId and also Name, AuthenticationType, etc… But how do I get my DisplayName for display?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Add the claim in ClaimsIdentity:
public class ApplicationUser : IdentityUser
{
...
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
userIdentity.AddClaim(new Claim("DisplayName", DisplayName));
return userIdentity;
}
}
Created an extention method to read DisplayName from ClaimsIdentity:
public static class IdentityExtensions
{
public static string GetDisplayName(this IIdentity identity)
{
if (identity == null)
{
throw new ArgumentNullException("identity");
}
var ci = identity as ClaimsIdentity;
if (ci != null)
{
return ci.FindFirstValue("DisplayName");
}
return null;
}
}
In your view use it like:
User.Identity.GetDisplayName()
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0
