简体   繁体   中英

After updating IdentityUser with custom properties using Web Forms. How to display new custom property

After following this page ( http://www.itorian.com/2013/11/customize-users-profile-in-aspnet.html ) I was able to add a custom property to my identity user without any problem. The problem I'm having is that after that everything that I can find on the web deals with displaying the custom properties in an MVC project, but my project is a 2015 ASP.NET Web Forms application. In the master page there is this slice of code to display the User Name :

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

The custom fields I created in the identity profile were FirstName and LastName. I'd like to be able to display them in the master page just like it is done in the code above, but instead display John Doe rather than JD_whateverLoginNameChosen. I just can't quite figure out how to access it properly. Thank you for your help.

UPDATE: I ended up accomplishing this by going into the code behind of the site.master page instead of trying to do it from the aspx side. I also had to change the link from an HTML link to an ASP HyperLink. Here is the code I used to do it:

        protected void Page_Load(object sender, EventArgs e)
    {
        var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
        var currentUser = manager.FindById(Context.User.Identity.GetUserId());

        HyperLink h = ((HyperLink)this.loginViewMaster.FindControl("ManageLink"));
        if(currentUser != null)
        {
            h.Text = "Hello, " + currentUser.FirstName + " " + currentUser.LastName;


        }


    }

I ended up accomplishing this by going into the code behind of the site.master page instead of trying to do it from the aspx side. I also had to change the link from an HTML link to an ASP HyperLink. Here is the code I used to do it:

        protected void Page_Load(object sender, EventArgs e)
{
    var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
    var currentUser = manager.FindById(Context.User.Identity.GetUserId());

    HyperLink h = ((HyperLink)this.loginViewMaster.FindControl("ManageLink"));
    if(currentUser != null)
    {
        h.Text = "Hello, " + currentUser.FirstName + " " + currentUser.LastName;


    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM