如何在MVC中用User.Identity.Name显示Name而不是UserName

本文关键字:Name 显示 UserName Identity MVC 中用 User | 更新日期: 2023-09-27 18:18:53

关于这个问题有很多话题,没有找到一个符合我的愿望。

在MVC5中,在_LoginPartial中,有这个函数User.Identity.GetUserName(),它显示实际用户的UserName。

我想显示ApplicationUser的Name,它包含以下属性:

public ApplicationUser()
{
   Name = FirstName + " " + LastName;
}
public FirstName {get;set;}
public LastName {get;set;}
public Name {get;}

我可以使用静态函数来获取Name,如:

function.Name(User.Identity.GetUserId())

我不想再次查询数据库,我想使用这个User.Identity.Name,但是这个函数返回UserName,我需要以某种方式覆盖。

如何使用User.Identity.Name获取Name

如何在MVC中用User.Identity.Name显示Name而不是UserName

User.FindFirst("name").Value

。搜索声明以获得显示字符串。

我的头UserManager有一个方法从UserId获取ApplicationUser。然后使用ApplicationUser对象,你可以获得Name属性。例如:

ApplicationUser user = UserManager.GetUserById(User.Identity.GetUserId());
user.Name;