【问题标题】:How to change the greeting message on ASP.Net from UserName to First Name or any other field?如何将 ASP.Net 上的问候消息从 UserName 更改为 First Name 或任何其他字段?
【发布时间】:2015-07-27 12:44:15
【问题描述】:
如何更改下面提到的代码以显示:Hello "First Name" 而不是 Hello "UserName"。
我已经在我的身份用户表中创建了一些自定义字段,例如“FirstName”、“MiddleName”、“LastName”。
下面提到的代码在 Site.Master 中。
<li>
<a runat="server" href="~/Account/Manage" title="Manage your account">
Hello, <%: Context.User.Identity.GetUserName() %> !
</a>
</li>
感谢您为解决我的问题所做的努力。
【问题讨论】:
标签:
c#
asp.net
webforms
asp.net-identity
【解决方案1】:
您必须将身份类型转换为您创建的自定义身份。所以您可以使用您在那里使用的字段。代码如下所示:
<li>
<a runat="server" href="~/Account/Manage" title="Manage your account">
Hello, <%: ((YourIdentity)Context.User.Identity).FirstName %> !
</a>
</li>
【解决方案2】:
首先在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>
【解决方案3】:
尝试使用
HttpContext.Current.User.Identity.Name
它应该可以工作
或者你应该试试
User.Identity.Name