Get the value of custom attributes in aspnet-Identity . NET CORE 2.0

Asked

Viewed 220 times

0

I’m using the Identity with aspnet core 2.0 and I needed to add the attribute nuCPF on the table Applicationuser. In the controller I am getting the user data as follows:

[Controller: Clientecontroller]

public ClienteController(UserManager<ApplicationUser> userManager)
{
    _userManager = userManager;
}

[HttpGet]
public ActionResult Index(int paginaAtual = 1, string filtro = "")
{
    var NomeUsuarioLogado = User.Identity.Name;
    var idUsuarioLogado = _userManager.GetUserId(HttpContext.User);

    xxxxx.... etc
}

The class Applicationuser is like this:

public class ApplicationUser
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public string NormalizedUserName { get; set; }
    public string Email { get; set; }
    public string NormalizedEmail { get; set; }
    public bool EmailConfirmed { get; set; }
    public string PasswordHash { get; set; }
    public string PhoneNumber { get; set; }
    public bool PhoneNumberConfirmed { get; set; }
    public bool TwoFactorEnabled { get; set; }
    public string nuCPF { get; set; }
}

Doubt 1:

How could I take the value of the field nuCPF?

Doubt 2:

When I use the '_userManager.Getuserid(user)', a select is made in the database to get the user id?

1 answer

0

To get all user information you must use:

var user = await _userManager.GetUserAsync(User);

Then just use user.NuCPF

And yes, he’s going to the bank to get the information.

OBS: change the name of nuCPF for NuCPF. By convention property names start with uppercase letters.

  • Fabri, it seems to me that there is no "Getuser" method in userManager. see how it is for me: https://prnt.sc/irn5jh ?

  • Getuserasync, use this one

  • You do not find the nuCPF property when using "user.Nucpf" , the following error appears: does not contain a definition for "nuCPF" and could not find any "nuCPF" extension method that accepts a first argument of type "Task<Applicationuser>"

  • Send the current code to see how this

  • 'public ClienteController(UserManager<ApplicationUser> userManager, ILogger<AccountController> logger)&#xA; {&#xA; _logger = logger;&#xA;&#xA; var user = userManager.GetUserAsync(User);&#xA; var teste = user.nuCPF;&#xA; }' the Applicationuser is just like the question in the main post.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.