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);
        }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            var repository = new ParseRepository(new WebCacheProvider(HttpContext.ApplicationInstance.Context));
            model.DeviceTypes =  repository.FindDeviceTypes();
            model.Countries = repository.FindCountriesNameAndId();
            if (ModelState.IsValid)
            {
                Exception logException = default(Exception);
                var userManager = new MyMentorUserManager(repository, Session);
                try
                {
                    model.CountryOfResidenceTitle = model.Countries[model.CountryOfResidence];


                    model.ContentTypeId = WorldIsRetriverFactory.GetWorldId(HttpContext, Session);
                    model.CurrencyId = (repository.FindResidenceById(model.CountryOfResidence)).CurrencyId;
                    await userManager.RegisterUser(model);

                    var parseUser = await userManager.Login(model.UserName, model.Password, Session);

                    Mailer.SendRegistrationSuccess(parseUser);
                    model.ShowSuccessMessage = true;
                    ViewBag.TeacherSuccessMessage = Strings.GetLocalizedString(Strings.RegistrationSuccessTeacher);
                    ViewBag.TeacherSuccessMessage = string.Format(ViewBag.TeacherSuccessMessage, model.FirstName);
                    ViewBag.StudentSuccessMessage = Strings.GetLocalizedString(Strings.RegistrationSuccessStudent);
                    ViewBag.StudentSuccessMessage = string.Format(ViewBag.StudentSuccessMessage, model.FirstName);
                }
                catch (ParseException ex)
                {
                    if (ex.Code == ParseException.ErrorCode.UsernameTaken)
                    {
                        ModelState.AddModelError("UserName", MyMentorResources.userExistsInTheSystem);
                    }
                    if (ex.Code == ParseException.ErrorCode.InvalidEmailAddress)
                    {
                        ModelState.AddModelError("UserName", MyMentorResources.illegalEmailAddress);
                    }
                    logException = ex;
                }
                catch (Exception ex)
                {
                    logException = ex;
                    mLogger.Log(LogLevel.Error, ex);

                    ModelState.AddModelError("_FORM", MyMentorResources.registrationGeneralError);
                }
                finally
                {
                    userManager.Dispose();
                }

                if (logException != null)
                {
                    await ParseLogger.Log("register new user", logException.ToString());
                }
            }
            else
            {
                var isCaptchaError = ModelState["CaptchaInputText"].Errors.Count > 0;
                ViewBag.IsCaptchaError = isCaptchaError;
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }