Update to original answer: (This violates the op’s first requirement, see my original answer if you have the same requirement) You can do it without modifying the claims and adding the extension file (in my original solution) by referencing FullName in the Razor View as:
@UserManager.GetUserAsync(User).Result.FullName
Original Answer:
This is pretty much just a shorter example of this stackoverflow question and following this tutorial.
Assuming you already have the property set up in the “ApplicationUser.cs” as well as the applicable ViewModels and Views for registration.
Example using “FullName” as extra property:
Modify the “AccountController.cs” Register Method to:
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var user = new ApplicationUser {
UserName = model.Email,
Email = model.Email,
FullName = model.FullName //<-ADDED PROPERTY HERE!!!
};
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
//ADD CLAIM HERE!!!!
await _userManager.AddClaimAsync(user, new Claim("FullName", user.FullName));
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(3, "User created a new account with password.");
return RedirectToLocal(returnUrl);
}
AddErrors(result);
}
return View(model);
}
And then I added a new file “Extensions/ClaimsPrincipalExtension.cs”
using System.Linq;
using System.Security.Claims;
namespace MyProject.Extensions
{
public static class ClaimsPrincipalExtension
{
public static string GetFullName(this ClaimsPrincipal principal)
{
var fullName = principal.Claims.FirstOrDefault(c => c.Type == "FullName");
return fullName?.Value;
}
}
}
and then in you views where you need to access the property add:
@using MyProject.Extensions
and call it when needed by:
@User.GetFullName()
The one problem with this is that I had to delete my current test user and then re-register in order see the “FullName” even though the database had the FullName property in it.