2015-10-06 86 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; 


    } 


}