如何将 ASP.Net 上的问候语从用户名更改为名字或任何其他字段?

How to change the greeting message on ASP.Net from UserName to First Name or any other field?

如何更改下面提到的代码以显示:Hello "First Name" 而不是 Hello "UserName".

我已经在我的身份用户 table 中创建了一些自定义字段,例如 "FirstName"、"MiddleName"、"LastName".

下面提到的代码在 Site.Master.

<li>
  <a runat="server" href="~/Account/Manage" title="Manage your account">
     Hello, <%: Context.User.Identity.GetUserName()  %> !
  </a>
</li>

感谢您为解决我的问题所做的努力。

尝试使用

HttpContext.Current.User.Identity.Name

它应该有效
或者你应该试试

User.Identity.Name

您必须将身份转换为您创建的自定义身份。这样您就可以使用您在此处使用的字段。代码如下所示:

<li>
  <a runat="server" href="~/Account/Manage" title="Manage your account">
     Hello, <%: ((YourIdentity)Context.User.Identity).FirstName  %> !
  </a>
</li>

首先在web.config

中添加Microsoft.AspNet.Identity.Owin命名空间
<configuration>
   <namespaces>
       <!-- other namespaces -->
       <add namespace="Microsoft.AspNet.Identity.Owin"/>
  </namespaces>
</configuration>

然后将您的代码替换为:

<li>
    <a runat="server" href="~/Account/Manage" title="Manage your account"> Hello,
        <%: HttpContext.Current.GetOwinContext()
            .GetUserManager<YourNamespace.ApplicationUserManager>()
            .FindById(Context.User.Identity.GetUserId()).FirstName %>!
   </a>
</li>