Search code examples
asp.netasp.net-membership

Storing User Information in Session with aspNetMembershipProvider


I'm developing an application in .NET mvc2. I'm using aspnetMembershipProvider for User registration and related activities. I need some custom information about user that I stored in a separate table (sysUser for example) and linked it to aspnetUser table through foreign key.

After login I need to fetch user's credentials from sysUser table and push it to the session. For this Account controller's Logon method seemed best to me and I pasted following code in my Logon ActionResult

 if (!ValidateLogOn(userName, password))
        {
            return View();
        }

        FormsAuth.SignIn(userName, rememberMe);
        ApplicationRepository _ApplicationRepository = new ApplicationRepository();
        MembershipUser aspUser = Membership.GetUser(userName);
        SessionUser CurrentUser = _ApplicationRepository.GetUserCredentials(aspUser.ProviderUserKey.ToString());

        //Session["CurrentUser"] = CurrentUser;

        if (!String.IsNullOrEmpty(returnUrl))
        {
            return Redirect(returnUrl);
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }

The code is working perfectly for me and put my desired information in the session but the thing is that if a user selects Remember me and on his next visit he won't have to Log in and I would not find my desired information in the Session. Where should I put my code that stores the user information in the session?


Solution

  • FormsAuthentication.SetAuthCookie(userName, saveLogin);
    

    MSDN Documentation for SetAuthCookie Method