How to change the greeting message on ASP.Net from

2019-09-17 22:42发布

How can I change the below mentioned code to show: Hello "First Name" instead of Hello "UserName".

I have already created some custom fields inside my Identity user table such as "FirstName","MiddleName","LastName".

Below mentioned code is inside Site.Master.

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

I appreciate your efforts in reaching a solution for my problem.

3条回答
Luminary・发光体
2楼-- · 2019-09-17 23:18

You will have to typecast the identity to the custom identity you made.. So you can use the field you used there. The code would look like this:

<li>
  <a runat="server" href="~/Account/Manage" title="Manage your account">
     Hello, <%: ((YourIdentity)Context.User.Identity).FirstName  %> !
  </a>
</li>
查看更多
孤傲高冷的网名
3楼-- · 2019-09-17 23:22

First Add Microsoft.AspNet.Identity.Owin namespace in web.config

<configuration>
   <namespaces>
       <!-- other namespaces -->
       <add namespace="Microsoft.AspNet.Identity.Owin"/>
  </namespaces>
</configuration>

then replace your code with:

<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>
查看更多
淡お忘
4楼-- · 2019-09-17 23:31

Try using

HttpContext.Current.User.Identity.Name

It should work
Or you should try

User.Identity.Name
查看更多
登录 后发表回答