ASP.NET Core Identity获取用户id

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/loongsking/article/details/79945368

使用Microsoft.AspNetCore.Identity命名空间
通过asp.net core Identity的api函数来获取用户id

代码如下
ApplicationUser user = await _userManager.GetUserAsync(HttpContext.User);
            string userId = user.Id;

完整的代码

 public class HomeController : Controller
    {
        private readonly UserManager<ApplicationUser> _userManager;

        public HomeController(UserManager<ApplicationUser> userManager)
        {
            _userManager = userManager;
        }
        public async Task<IActionResult> Index()
        {
            ApplicationUser user = await _userManager.GetUserAsync(HttpContext.User);
            string userId = user.Id;
            //User.Identity.
            return View();
        }
}

阅读更多

没有更多推荐了,返回首页