简体   繁体   中英

How can I access custom IdentityUser data (Img Name) inside the LoginPartial (.Net Core 2.2)

I was wondering how I can display the custom data I've used to extend the IdentityUser.

By default the loginpartial displays User.Identity.Name (which is actually an e-mail address), I would like to display the user's fullname and display an user image with it. It calls User.Identity.Name from interface IIdentity

    namespace System.Security.Principal
{ 
    public interface IIdentity
    {   
        string AuthenticationType { get; }
        bool IsAuthenticated { get; }
        string Name { get; }
    }
}

I can't change this file though. This is my class extending the identityuser

    public class ApplicationUser : IdentityUser
{
    public ApplicationUser() : base() { } 
    public string Voornaam { get; set; }
    public string Tussenvoegsel { get; set; }
    public string Achternaam { get; set; }
    public string Gebruikersnummer { get; set; }
    public string ImgNaam { get; set; }
}

in the partial view I call for this model class aswell:

@using Microsoft.AspNetCore.Identity
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

Now my question is, how can I retreive string ImgNaam inside of my partial view?

I think it would be better to store the information you need in Claims ( you can read this post about it ).

But you also can get information using UserManager .

You can use User variable which has ClaimsPrincipal type to get the whole user from UserManager using the following code:

@
{
     var activeUser = await UserManager.GetUserAsync(User);
}

After this, you can use this variable:


   <img src="@activeUser.ImgNaam">

You can do as follows:

@using Microsoft.AspNetCore.Identity
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

@{
   ApplicationUser loggedInUser = await UserManager.GetUserAsync(User); // Here `User` is the claim principal
   var imgNaam= loggedInUser.ImgNaam;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM