首页 > 解决方案 > 如何获取当前登录用户的详细信息?

问题描述

我添加到新属性ApplictionUser,现在我想检索它?

public class ApplicationUser : IdentityUser
{
   [Required]
   [StringLength(50)]
   public string FirstName { get; set; }

   [Required]
   [StringLength(50)]
   public string LastName { get; set; }

   //....
}

我试图通过使用检索,User.Identity...但我无法获得名字和姓氏?

怎么做 ?我将非常感激

标签: c#asp.net-mvc

解决方案


我以这种方式检索了 firstName 和 SecondName:

步骤1。

// DbContext.
private readonly ApplicationDbContext _context;

public HomeController()
{
   _context = new ApplicationDbContext();
}

第2步。

// Getting Id of Current LoggedIn User.
var UserId = User.Identity.GetUserId();

步骤 3。

var User = _context.Users.Single(user => user.Id == UserId);

这些步骤对我有用。


推荐阅读