protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            HttpContext context = HttpContext.Current;
            if (context != null && context.Session != null)
            {

                var userContextFactory = new UserContextFactory();
                var appContextFactory = new DefaultAppContextFactory();
                if (User.Identity.IsAuthenticated)
                {
                    context.Session["UserContext"] = userContextFactory.CreateUserContext(User.Identity.Name,
                        User.Identity.Name);

                }
                else
                {
                    context.Session["UserContext"] = userContextFactory.CreateUserContext();
                }
             
                context.Session["AppContext"] = appContextFactory.CreateAppContext();

            }
        }
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
            switch (result)
            {
                case SignInStatus.Success:


                    //user is validated, set user context
                    var userContextFactory = new UserContextFactory();
                    var appContextFactory = new DefaultAppContextFactory();

                    Session["UserContext"] = userContextFactory.CreateUserContext(model.Email, model.Email);
                    Session["AppContext"] = appContextFactory.CreateAppContext();


                    return RedirectToLocal(returnUrl);
                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);
            }
        }