Get user id in ASP.NET Core 2

前端 未结 3 602
慢半拍i
慢半拍i 2021-01-12 03:19

I\'m trying to get the user id in an ASP.NET Core 2.1 MVC project.

However, I was only able to get the email. I\'m almost sure there has to be a 1/2 line way to get

3条回答
  •  孤街浪徒
    2021-01-12 04:04

    In Asp.net Core 2.2, I solved this problem with the following piece of code.

    using Microsoft.AspNetCore.Identity;
    var user = await _userManager.FindByEmailAsync(User.Identity.Name);
    

    This way you will get the user information by his email. User.Identity.Name will provide you the email address of the current logged in user.

    I hope this will be useful to someone.

提交回复
热议问题