Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 4 How does Membership "Remember me" work?

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

like image 811
Javier Avatar asked Nov 27 '13 23:11

Javier


1 Answers

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";
        }
    }
}
like image 90
Win Avatar answered Nov 13 '22 10:11

Win