The following is the LogOn user control from a standard 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 a master page:
<div id="logindisplay">
<% Html.RenderPartial("LogOnUserControl"); %>
</div>
The <%: Page.User.Identity.Name %>
code displays the login name of the user, currently logged in.
How to display the user's FirstName
instead, which is saved in the Profile?
We can read it in a controller like following:
ViewData["FirstName"] = AccountProfile.CurrentUser.FirstName;
If we, for example, try to do this way:
<%: ViewData["FirstName"] %>
It renders only on the page which was called by the controller where the ViewData["FirstName"]
value was assigned.