I have 2 controllers. A HomeController and an AccountController. I have a _Layout.cshtml and a partial view _Login.cshtml that gets rendered in the header section of the _Layout view. The HomeController has the default Index() method.
The very first time, the user sees Have an Account? Log In. Log In is a link that points to the Login method in the AccountController. The Logon view contains UserName, Password fields, and a Check box for Remember me. When the user logs in for the first time and has the Remember me checked, I call my MySecurityRepository to: validate the user and go get basic info for their profile (address, phone number, email, etc..) and the UserInfo object is populated and the header shows the UserName as a link. This all works.
The user closes the browser and reopens the site. I see the user is already logged in, Request.IsAuthenticated is true, but how do I go about retrieving the user’s profile again and caching?
Here is what the _Login partial view looks like…
<div id="login">
@if (Request.IsAuthenticated)
{
<text>
[@Html.ActionLink((HttpContext.Current.Session["UserInfo"] as UserInfo).FirstName, "Profile", "Account")]
[@Html.ActionLink("Log Off", "LogOff", "Account")]
</text>
}
else
{
<span>Have an account?</span>
@Html.ActionLink("Log In", "LogOn", "Account")
} </div>
Thanks.
You will have to fetch the user information from the backend if it is not available. For this you could write a child action that will first look at the session and if it is not available there fetch it and store it inside. Then you could include this child action in the
_Layoutusing theHtml.Actionhelper.