public static bool LoginToFogBugz(string fogBugzUrl, string fogBugzUsername, string fogBugzPassword)
        {
            UserContext.FbAccountId = null;
            UserContext.IsLoggedIn = false;

            string cacheKey = MsCacheKey.Gen(MsCacheDataType.FogBugz_ClientByFogBugzUrl, fogBugzUrl);

            object existingFbClient = null;
            string fogBugzApiUrl = UrlUtils.GenFogBugzApiUrl(fogBugzUrl);

            if (!MsCache.TryGet(cacheKey, ref existingFbClient))
            {
                FogBugzClientEx fbClient = new FogBugzClientEx(){Listener = new CaseCreationListener()};
                try
                {
                    //This function may throw exception which may need to be handled by calling method
                    fbClient.LogOn(fogBugzApiUrl, fogBugzUsername, fogBugzPassword);
                }
                catch (Exception)
                {
                    return false;
                }

                MsCache.Set(cacheKey, fbClient);
            }
            else
            {
                if (!FogBugzClientEx.IsValidLogin(fogBugzApiUrl, fogBugzUsername, fogBugzPassword))
                {
                    return false;
                }
            }

            //Set context account
            var fbAccount = new FbAccountService().Ensure(fogBugzUrl);

            UserContext.FbAccountId = fbAccount.Id;
            UserContext.IsLoggedIn = true;

            return true;
        }
        public ActionResult Settings(SettingViewModel viewModel)
        {
            viewModel.Setup();

            if (!ModelState.IsValid)
            {
                return View("Update", viewModel);
            }

            var fbAccount = DbContext.FbAccounts
                                    .Include("Settings")
                                    .Include("Settings.TestRailConfig")
                                    .SingleOrDefault(s => s.Id == viewModel.Id);

            FbAccountSettings fbAccountSettings;

            if (fbAccount == null)
                fbAccountSettings = new FbAccountSettings();
            else
                fbAccountSettings = fbAccount.Settings;

            //refresh cache when qa estimate settings changed
            if (fbAccountSettings.AllowQaEstimates != viewModel.AllowQaEstimates
                || fbAccountSettings.QaEstimateCustomFieldName != viewModel.QaEstimateCustomFieldname
                || fbAccountSettings.QaPercentage != viewModel.QaPercentage)
            {
                CleareCachedCaseSets();
            }

            MapSettingsEntity(fbAccountSettings, viewModel);

            var service = new FbAccountService();

            if (fbAccount != null && fbAccount.Settings != null)
            {
                if (fbAccount.Settings.SendDailyDigestEmails)
                {
                    string cacheKeyToken = MsCacheKey.Gen(MsCacheDataType.FogBugz_ClientByFogBugzUrl, UserContext.FogBugzUrl);
                    object fbClientObject = null;
                    MsCache.TryGet(cacheKeyToken, ref fbClientObject);
                    var fbClient = (FogBugzClientEx) fbClientObject;
                    fbAccount.Token = fbClient.GetToken();
                }
                else
                {
                    fbAccount.Token = String.Empty;
                }
            }

            // save settings in db
            service.Update(fbAccount);

            var cacheKey = MsCacheKey.Gen(MsCacheDataType.ProjectLists, UserContext.FogBugzUrl);
            MsCache.TryRemove(cacheKey);

            cacheKey = MsCacheKey.Gen(MsCacheDataType.FogBugz_ClientCacheByFogBugzUrl, UserContext.FogBugzUrl);
            MsCache.TryRemove(cacheKey);

            UserContext.SetNotification(PageNotificationType.Success, "Settings have been updated.");

            return RedirectToAction("Settings");
        }