Store Additional User’s Data In Claims In ASP.Net Core Identity



Published On Thursday June 4, 2020
Reading Time: 2 minutes

What if you want to add additional parameter or field in the user identity table i.e. AspNetUser. We might need this one so lets look how we can achieve it.

  • Open Models/ApplicationUser.cs and add the following line of code. We can add much more field so for now only a field is added as ContactName
public class ApplicationUser : IdentityUser
{
   public string ContactName { get; set; }
}
  • Once we have added the code we need to carry out a migration and then update the database as:
dotnet ef migrations add ContactNameField
dotnet ef database update

Once done our database is updated with the new field added as ContactName

  • Now open up Models/AccountViewModels/RegisterViewModel.cs and add the following code
[Required]
[Display(Name = "Name")]
public string ContactName { get; set; }
  • Then reach out to Views/Account/Register.chstml and add up the following code
<div class="form-group">
     <label asp-for="ContactName"></label>
     <input asp-for="ContactName" class="form-control" />
     <span asp-validation-for="ContactName" class="text-danger"></span>
</div>
  • Open up Controllers/AccountController then add the following code
var user = new ApplicationUser { ContactName=model.ContactName, UserName = model.Email, Email = model.Email };

Once Completed Do The Same For:

  • View/Manage/Index.cshtml,
  • Models/ManageViewModels/IndexViewModel.cs and in
  • Index method in ManageControler class.

Now, we need to add the new data to claim for this create a new class with the code below

public class MyUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>
    {
        public MyUserClaimsPrincipalFactory(
            UserManager<ApplicationUser> userManager,
            RoleManager<IdentityRole> roleManager,
            IOptions<IdentityOptions> optionsAccessor)
            : base(userManager, roleManager, optionsAccessor) {
        }

        protected override async Task<ClaimsIdentity> GenerateClaimsAsync(ApplicationUser user) {
            var identity = await base.GenerateClaimsAsync(user);
            identity.AddClaim(new Claim("ContactName", user.ContactName ?? ""));
            return identity;
        }
    }

Now we need to register it in DI container in ConfigureServices  methods of the Startup class as

public void ConfigureServices(IServiceCollection services) {
   .
   .
    services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();
     
    //add the following line of code
    services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, MyUserClaimsPrincipalFactory>();
    .
    .
}

So, this is all about the setup. Now for accessing the data in view it can be done by a single line of code as:

@(User.FindFirst("ContactName").Value)

Ref: korzh.com

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x