Not getting the correct core.net User Id

Asked

Viewed 38 times

0

I’m using ASP.NET CORE 2.0, and I need to get the logged in user id. This is my Account Controller:

[Route("[controller]/[action]")]
public class AccountController : Controller
{
    private readonly SignInManager<ApplicationUser> _signInManager;
    private readonly ILogger _logger;
    private readonly UserManager<ApplicationUser> _userManager;

    public AccountController(SignInManager<ApplicationUser> signInManager, ILogger<AccountController> logger, UserManager<ApplicationUser> userManager)
    {
        _signInManager = signInManager;
        _logger = logger;
        _userManager = userManager;
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Logout()
    {
        await _signInManager.SignOutAsync();
        _logger.LogInformation("User logged out.");
        return RedirectToPage("./Index");
    }


}

I’m trying to get the id this way:

 ApplicationUser a = new ApplicationUser();
            CR.FuncionarioId =  a.Id;

However, this is the id that is in the bank:

5e7d4078-6388-43e5-8dac-4378f5b366bc

But it is returning me a completely different id, because this is happening? Utilizo Identity. There when I use a.Id, every hour that I soon, even with the same user, it brings a different Id, and is not bringing the Id that is registered in the Aspnetusers table.

  • It happens because every hour you give a new Applicationuser(), it is obvious that every call will create a new ID, you have to load the ID of the logged in user, not create a whole time.

  • And how I do it ?

  • 1

    var user = await _signInManager.GetUserAsync(User); and then CR.FuncionarioId = user.Id;

No answers

Browser other questions tagged

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