public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var repository = RepositoryFactory.GetInstance(Session);
                var logException = default(Exception);
                var userManager = new MyMentorUserManager(repository, Session);

                try
                {
                    var user = await userManager.Login(model.UserName, model.Password, Session);
                    var adminData = user.GetPointerObject<UserAdminData>("adminData");
                    user.GetPointerObject<BL.Models.SugNirut>("sugNirut");
                    var userStatusObj = adminData.GetPointerObject<ParseObject>("userStatus");
                    var userStatus = userStatusObj.GetString("status");
                    var userStatusTitle = userStatusObj.GetLocalizedField("statusTitle");

                    var prohibitedStatuses = new[] {"blocked", "hold"};

                    if (prohibitedStatuses.Contains(userStatus))
                    {
                        ParseUser.LogOut();
                        Session["user"] = null;
                        ModelState.AddModelError("",
                            string.Format(MyMentorResources.userNotAllowedToLogin, userStatusTitle));
                        return View(model);
                    }
                    if (userStatus == UserStatusStrings.AppUser)
                    {
                        return RedirectToAction("UpdateStudent");
                    }

                    var prefferences = new UserPrefferences
                    {
                        SelectedLanguage = user.GetPointerObjectId("interfaceLanguage"),
                        SelectedContentType = user.GetPointerObjectId("contentType"),
                        SelectedCurrency = user.GetPointerObjectId("currency")
                    };
                    var prefferencesManager = new UserPrefferencesManager(repository, HttpContext);
                    prefferencesManager.SetUserPrefferences(prefferences, Session.GetLoggedInUser() == null);

                    SetPurchases(repository, user);
                    if (model.RememberMe)
                    {
                        var ticket = new FormsAuthenticationTicket(1,
                            model.UserName,
                            DateTime.Now,
                            DateTime.Now.AddMinutes(30),
                            true,
                            model.Password,
                            FormsAuthentication.FormsCookiePath);

                        string encTicket = FormsAuthentication.Encrypt(ticket);

                        // Create the cookie.
                        var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                        authCookie.Expires = DateTime.Now.AddYears(1);
                        Response.Cookies.Add(authCookie);
                    }
                    var webCacheProvider = new WebCacheProvider(System.Web.HttpContext.Current);
                    var currencyRatesService = new CurrencyRatesService(repository, webCacheProvider,System.Configuration.ConfigurationManager.AppSettings["bankServiceUrl"]);
                    currencyRatesService.UpdateRates();
                    
                }
                catch (Exception ex)
                {
                    logException = ex;
                    mLogger.Log(LogLevel.Error, ex);
                    ModelState.AddModelError("", MyMentorResources.passwordIncorrect);
                }
                finally
                {
                    repository.Dispose();
                }

                if (logException != null)
                {
                    await ParseLogger.Log("login", logException);
                }

                if (Session.GetLoggedInUser() != null)
                {
                    return RedirectToLocal(returnUrl);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
 private void SetUserPrefferences(UserPrefferences prefferences)
 {
     using (var repository = RepositoryFactory.GetInstance(Session))
     {
         var userPrefferencesManager = new UserPrefferencesManager(repository, HttpContext);
         userPrefferencesManager.SetUserPrefferences(prefferences, Session.GetLoggedInUser() == null);    
     }
 }