Home > Back-end >  How to access custom ApplicationUser properties from view in ASP.net core 6?
How to access custom ApplicationUser properties from view in ASP.net core 6?

Time:08-01

I have just added custom properties to the ApplicationUser class, which has worked and the database is now storing those properties. However, I am not sure how to display these properties on the MVC view.

This is my application user class:

using System;
using Microsoft.AspNetCore.Identity;

namespace IssueTracker.Areas.Identity.Data
{
    public class ApplicationUser : IdentityUser
    {
        public String? FirstName { get; set; }

        public String? LastName { get; set; }

        public int? RoleNumber { get; set; }

    }
}

Originally, my _LoginPartial.cshtml references the Identity name through this line:

@User.Identity?.Name

How can I change this to show the FirstName property? Additionally, how can I change this so that I can access all 3 properties from the ApplicationUser class on any view or partial view?

I have tried looking at other posts, but most are outdated and did not work. Thank you! I am new to MVC, so forgive me if my question is simple or if I come across as a starter.

CodePudding user response:

I was having the same problem with you. This is how I access it.

First you need to register this under the Program.cs before the builder.Build();

builder.Services.AddHttpContextAccessor();

This is the static class that I use to retrieve the additional info.

  public  class Common
    {
    public static string? GetFirstName(WorkshopProContext db) //Refer to you own db context
            {
    
                HttpContextAccessor httpContextAccessor = new HttpContextAccessor();
    
                var userId = httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
    
    
                var optionsBuilder = new DbContextOptionsBuilder<WorkshopProContext>();
    
    
    
                ApplicationUser AppUser = (ApplicationUser)db.Users.Where(a => a.Id == userId).FirstOrDefault();
    
                return AppUser.FirstName;
    
            }
}

This is how I inject the info to the partial View

@using Microsoft.AspNetCore.Identity
@using WorkshopPro.Areas.Identity.Data
@using WorkshopPro.Helper;  @* retrieve the Helper class*@
@using WorkshopPro.Data;    @* retrieve the DbContext class*@


@inject WorkshopProContext db; @* Inject Db Context *@
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

<ul >
@if (SignInManager.IsSignedIn(User))
{

        <a>@Common.GetFirstName(db)</a> @* Display First Name*@
    
   
}
else
{
    
}

CodePudding user response:

You can inject UserManager<T> into your view, Then get the value. refer to this simple demo:

@inject  UserManager<ApplicationUser> userManager;


<h2>
@foreach (var item in userManager.Users)
  {
     //Then you can get the value of these proerties.
      @item.FirstName
      @items.LastName 
      @items.RoleNumber
      
  }
</h2>

Mode details you can refer to Dependency injection into views in ASP.NET Core

  • Related