2015-10-06 81 views
1

在关注此页面(http://www.itorian.com/2013/11/customize-users-profile-in-aspnet.html)之后,我可以将自定义属性添加到我的身份用户,没有任何问题。我遇到的问题是,在网络上可以找到的所有内容都是在MVC项目中显示自定义属性,但我的项目是2015 ASP.NET Web Forms应用程序。在母版页有这个片的代码来显示用户名:使用Web窗体使用自定义属性更新IdentityUser之后。如何显示新的自定义属性

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

我的身份配置文件中创建了名字和姓氏的自定义字段。我希望能够像在上面的代码中那样在master页面中显示它们,而是显示John Doe而不是JD_whateverLoginNameChosen。我只是无法弄清楚如何正确访问它。感谢您的帮助。

更新: 我最终通过进入site.master页面后面的代码而不是尝试从aspx端执行此操作来完成此操作。我还必须更改从HTML链接到ASP HyperLink的链接。下面是我用来做它的代码:我最终通过进入代码的Site.Master页的后面,而不是试图从ASPX边做完成这个

 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; 


     } 


    } 
+0

尝试'((ApplicationUser)Context.User).FirstName'&'((ApplicationUser)Context.User).LastName'; – vendettamit

回答

2

。我还必须更改从HTML链接到ASP HyperLink的链接。这里是我用来做它的代码:

 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; 


    } 


}