7

I extended the ApplicationUser class to have 2 extra properties, FirstName and LastName. Both properties are persisted correctly in the database.

public class ApplicationUser : IdentityUser
{
    [Required]
    [StringLength(100)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(100)]
    public string LastName { get; set; }
}

I am following a simple beginner example, and the code in question is as generated by the New Project with User Authentication command:

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

@if (SignInManager.IsSignedIn(User))
{
    <form asp-area="" asp-controller="Account" asp-action="Logout" method="post" id="logoutForm" class="navbar-right">
        <ul class="nav navbar-nav navbar-right">
            <li>
                <a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">Hello @UserManager.GetUserName(User)!</a>
            </li>
            <li>
                <button type="submit" class="btn btn-link navbar-btn navbar-link">Log out</button>
            </li>
        </ul>
    </form>
}

And instead of Hello @UserManager.GetUserName(User)! I would like to say Hello ???????.FirstName!

I cannot find a way to access those extra properties from the view. Any suggestions on how to do that?

Thanks!

0

1 Answer 1

12

You need to inject a UserManager into your service (or view in your case). This has a generic type parameter which in your case would be ApplicationUser:

UserManager<ApplicationUser> userManager;

You can then query users via the UserManager.Users property. Alternatively if you need to get the current user and have their ClaimsPrincipal you can use the following code to retrieve the corresponding ApplicationUser:

ApplicationUser user = await UserManager.GetUserAsync(claimsPrincipal);
string firstName = user.FirstName;

Although I (personally) am not so keen on it, .Net Core supports injection into views using the following syntax.

@inject UserManager<ApplicstionUser> UserManager
5
  • I see that I already have a User injected, which is of type ClaimsPrincipal. How would a query for the user attributes would look like?
    – H Mihail
    Nov 6, 2017 at 8:05
  • 1
    @HMihail: I have updated my answer with regards to using ClaimsPrincipal. Can you please update your question with your code? Nov 6, 2017 at 14:02
  • 5
    @HMihail You can just use Hello @UserManager.GetUserAsync(User).Result?.FirstName although it might be nicer to get the ApplicationUser first and assign to a variable. Nov 6, 2017 at 16:43
  • I love you @CalC. So easy...wading through documentation for hours to get the above line of code.
    – infocyde
    Jan 30, 2018 at 23:34
  • 1
    Glad it helped @infocyde ☺️ Jan 30, 2018 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.