0

I cannot figure out when after login to an app is the session first time available along with user authentication. My goal is after a user logs in to the app to save some data about the user into session (right away after the login, before anything else happens), so I can use it later. I use the default MVC 5 template with VS2015. My login looks like

var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        switch (result)
        {
            case SignInStatus.Success:
                return RedirectToLocal(returnUrl); //I jump here
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
        }

private ActionResult RedirectToLocal(string returnUrl)
    {
        //here I would like to save some stuff into the session, the session is available here but the user is not authenticated yet

        if (Url.IsLocalUrl(returnUrl))
        {
            return Redirect(returnUrl);
        }
        return RedirectToAction("Index", "Course");
    }

Ok, next I tried global.asax:

protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        //and of course here I have authenticated user but the session is null
    }

Can you help me out, please?

Petr Žampach
  • 95
  • 1
  • 1
  • 13

1 Answers1

1

The ASP is going to be more stateless than before and it means it's better not to use state based objects like session.If you are using MVC5, you are using identity 2. It means you have ApplicationUser in your identity context. So you have almost every thing you need by using ApplicationUser. If you need something more ApplicationUser, the best approach is extending ApplicationUser. In that case you don't need any session variables at all. Check here as an example.

Community
  • 1
  • 1
Hadee
  • 1,392
  • 1
  • 14
  • 25