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

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

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

The code below 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 to achieve a solution to my problem.

0
source share
3 answers

First add the Microsoft.AspNet.Identity.Owin namespace to 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> 
+1
source

You will need to give the identifier the identity of the personal identity that you made. Therefore, you can use the field that you used there. The code will look like this:

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

Try using

 HttpContext.Current.User.Identity.Name 

He should work
Or you should try

 User.Identity.Name 
0
source

All Articles