The following is a custom LogOn element from the default default ASP.NET MVC project created by Visual Studio ( LogOnUserControl.ascx ):
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> <% if (Request.IsAuthenticated) { %> Welcome <b><%: Page.User.Identity.Name %></b>! [ <%: Html.ActionLink("Log Off", "LogOff", "Account") %> ] <% } else { %> [ <%: Html.ActionLink("Log On", "LogOn", "Account")%> ] <% } %>
which is inserted into the main page:
<div id="logindisplay"> <% Html.RenderPartial("LogOnUserControl"); %> </div>
The code <%: Page.User.Identity.Name %> displays the username of the user who is currently logged on.
How to display the FirstName user instead, which is saved in the profile?
We can read it in the controller as follows:
ViewData["FirstName"] = AccountProfile.CurrentUser.FirstName;
If we, for example, try to do this:
<%: ViewData["FirstName"] %>
It is displayed only on the page that was called by the controller, where the value ViewData["FirstName"] was assigned.
asp.net-mvc user-controls master-pages
rem
source share