I am new at asp.net Identity 2.0 and I want to show my FullName in Razor view page instade of Username.
So I added new property to IdentityUser.
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName => $"{FirstName} {LastName}";
}
Default AccountController contains a Login Method:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
I edited this login method
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
//-------------------------------------------------------
if (result == SignInStatus.Success)
{
var user = await UserManager.FindByEmailAsync(model.Email);
if (user != null)
{
await UserManager.AddClaimAsync(user.Id, new Claim("FullName", user.FullName));
}
}
//--------------------------------------------------------
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
And I created an extention method to read FullName from Razor view:
public static class IdentityExtensions
{
public static string GetFullName(this IIdentity identity)
{
var claim = ((ClaimsIdentity) identity);
return claim.FindFirst("FullName");
}
}
But FullName allways coming Null
<ul class="nav navbar-nav navbar-right">
<li>
@Html.ActionLink("Hello " + User.Identity.GetFullName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
</li>
<li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
The way you are trying to add the claim, you are creating an entry to database, in UserClaims
table. If you want to do this then you have to add the claim (await UserManager.AddClaimAsync(user.Id, new Claim("FullName", user.FullName));
) before PasswordSignInAsync
and in my opinion not in Login action. Its better inside the action where you add and update the User's FirstName and LastName.
An other way is to add this data when ClaimsIdentity
is generated on login. Inside your custom IdentityUser
class there is a GenerateUserIdentityAsync
method where you can simply:
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("LastName", $"{FirstName} {LastName}"));
return userIdentity;
}
}