views:

152

answers:

2

I was told that I can create ActionResult in any Controller and return PartialView with User.Identity.Name. I can then RenderPartialView in my MasterPage in order to display the user's name. I was trying to implement it, but kept getting an error that the system is not seeing my PartialView. Can someone please please explain in detail how that can be implemented? Thank you very much

Unfortunately the way this project is structured, Logon is not being rendered from within the Master. It's called from WebConfig and authenticated in AccountController.

<authentication mode="Forms"> 
      <forms loginUrl="~/Account" timeout="2880"/> 
</authentication>
+6  A: 

You already have a working example in the basic template that VS pop for you when you create a new MVC project... in the site.master you have an Html.RenderPartial("LogOnUserControl") that renders the LogOnUserControl partial view (located in the Views/Shared folder)... that controls shows the User.Identity.Name if the user is authenticated...

Unless this is not what you mean in which case I think we need a bit more explanation on your question :)

Jaime
+4  A: 

When you create a new MVC project, if you look at the LogOnUserControl.ascx file, you'll notice that this line of code is executed when Request.IsAuthenticated is true...

<%= Html.Encode(Page.User.Identity.Name) %>

From the Site.Master inside of the Views\Shared directory, you'll see the LogOnUserControl is rendered to the page like this:

<div id="logindisplay">
    <% Html.RenderPartial("LogOnUserControl"); %>
</div>
RSolberg
Unfortunately the way this project is structured, Logon is not being rendered from within the Master. It's called from WebConfig and authenticated in AccountController.<authentication mode="Forms"> <forms loginUrl="~/Account" timeout="2880"/></authentication>
Edward N.
Edward: Your question specifically calls out wanting to "render" from the master page the name of the person logged on. I see no difference with the code you provided and mine... Here is my web.config setting... <authentication mode="Forms"> <forms loginUrl="~/Account/LogOn" timeout="2880" /> </authentication>
RSolberg
<%= Html.Encode(Page.User.Identity.Name) %> worked perfectly. Thanks!!!Is there a way to retrieve user's full name?
Edward N.
Edward: I think that will require some custom code of some sort to access that information.
RSolberg
Thanks. I think I'll just keep the userid as you mentioned. Thanks for your help!
Edward N.