5

I'm using the standard membership feature in ASP.NET MVC 4.

I have a login form that when the user logs in successfully, some user information is retrieved from the database and put in a Session variable which I use in certain parts of the application.

My problem is the following:

When I activate the "Remember me" option it works fine, but the session variable won't never be loaded because this one is actually being loaded during the login validation process, which is now being skipped.

My question would be: is there any way to catch the "Remember me" validation process to put my custom code which will load the session variable I need? (or maybe adding more information to the "remember me" cookie which I can use later?)

Thank you

Javier
  • 2,093
  • 35
  • 50
  • Correct me from wrong, what you are doing is when log in you return back an object that contains some info and put it in a session. After you create auth cookies, set expiration date and put it in a response. You also set a timeout on the form auth element in the web.config(let's say 5 days). Now you can bypass auth by sending auth cookies. If it's true I believe your object should be still stored in the session? Question is do you need the object or you need a new one even using remember me? – Anton Nov 27 '13 at 23:33

1 Answers1

5

In your scenario, you can use Global.asax's Session_Start event to fill the User's information.

public class Global : System.Web.HttpApplication
{
    protected void Session_Start(object sender, EventArgs e)
    {
        if (HttpContext.Current.User != null &&
            HttpContext.Current.User.Identity.IsAuthenticated &&
            HttpContext.Current.Session["MyUserInfo"] == null)
        {
            // Get the user's information from database and save it to Session.
            HttpContext.Current.Session["MyUserInfo"] = "johndoe";
        }
    }
}
Win
  • 61,100
  • 13
  • 102
  • 181