c# - 如何获取当前登录用户的详细信息?

标签 c# asp.net-mvc

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

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

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

   //....
}

I tried to retrieved by using User.Identity... but i can't get first&last names?

怎么做?我将非常感激

最佳答案

我以这种方式检索了 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);

These steps worked for me.

关于c# - 如何获取当前登录用户的详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54616897/

相关文章:

c# - ASP.NET MVC 客户端验证消息错误

c# - ConfigurationManager.ConnectionStrings.ConnectionString 问题

asp.net - 当用户未通过身份验证时,如何处理 ajax 请求?

c# - 本地化 ASP.NET Identity 错误消息

c# - 什么时候需要在 C# 中使用 stackalloc 关键字?

asp.net-mvc - Ninject、MVC 和 session 范围 : Reasonable Practice?

asp.net-mvc - 属性如何阻止 MVC 操作的执行?

c# - 以编程方式绑定(bind) DataGridTemplateColumn

c# - 如果另一条消息即将说出,如何停止 SpeakAsync?

c# - 创建方法可以访问类似的命名字段,但没有继承的不同类型的对象