private static DispatchStepResult HandleLanguagePost(RequestContext requestContext, CultureInfo culture, string timeZoneKeyName, bool isOptimized, string destination)
        {
            ExTraceGlobals.CoreCallTracer.TraceDebug(0L, "[RequestDispatcher::HandleLanguagePost] entry.");
            if (requestContext.UserContext == null)
            {
                throw new OwaInvalidOperationException("UserContext should be created by the time language post is handled");
            }
            requestContext.LanguagePostUserCulture = culture;
            requestContext.HttpContext.Response.Cookies.Set(new HttpCookie("mkt", culture.Name));
            if (!string.IsNullOrWhiteSpace(destination) && (destination.StartsWith("/ecp/", StringComparison.OrdinalIgnoreCase) || destination.StartsWith("/owa/", StringComparison.OrdinalIgnoreCase)))
            {
                requestContext.DestinationUrl = destination;
            }
            else
            {
                requestContext.DestinationUrl = string.Empty;
            }
            Culture.InternalSetThreadPreferredCulture(culture);
            MailboxSession mailboxSession = null;

            try
            {
                ExTraceGlobals.CoreCallTracer.TraceDebug <string, bool>(0L, "[RequestDispatcher::HandleLanguagePost] Attempting to save the timeZoneKeyName (tzid={0}) and isOptimized={1} in the mailbox.", timeZoneKeyName, isOptimized);
                OwaIdentity logonIdentity = requestContext.UserContext.LogonIdentity;
                mailboxSession = logonIdentity.CreateMailboxSession(requestContext.UserContext.ExchangePrincipal, culture);
                if (requestContext.UserContext.IsExplicitLogon && !mailboxSession.CanActAsOwner)
                {
                    throw new OwaExplicitLogonException("User has no access rights to the mailbox", "ErrorExplicitLogonAccessDenied");
                }
                try
                {
                    ExTraceGlobals.CoreCallTracer.TraceDebug <int>(0L, "[RequestDispatcher::HandleLanguagePost] Trying to save the culture to the AD (lcid={0})", culture.LCID);
                    PreferredCultures preferredCultures = new PreferredCultures(requestContext.UserContext.ExchangePrincipal.PreferredCultures);
                    preferredCultures.AddSupportedCulture(culture, new Predicate <CultureInfo>(ClientCultures.IsSupportedCulture));
                    Culture.SetPreferredCulture(requestContext.UserContext.ExchangePrincipal, preferredCultures, mailboxSession.GetADRecipientSession(false, ConsistencyMode.FullyConsistent));
                    requestContext.UserContext.ExchangePrincipal = requestContext.UserContext.ExchangePrincipal.WithPreferredCultures(preferredCultures);
                }
                catch (Exception ex)
                {
                    if (!(ex is ADOperationException) && !(ex is InvalidOperationException))
                    {
                        throw;
                    }
                    requestContext.FailedToSaveUserCulture = true;
                    if (ExTraceGlobals.CoreCallTracer.IsTraceEnabled(TraceType.ErrorTrace))
                    {
                        StringBuilder stringBuilder = new StringBuilder("Failed to save user's culture in the AD.");
                        stringBuilder.Append("\n\nException: ");
                        stringBuilder.Append(ex.GetType().ToString());
                        stringBuilder.Append("\n");
                        stringBuilder.Append(ex.Message);
                        stringBuilder.Append(")");
                        if (!string.IsNullOrEmpty(ex.StackTrace))
                        {
                            stringBuilder.Append("\n\nCallstack:\n");
                            stringBuilder.Append(ex.StackTrace);
                        }
                        ExTraceGlobals.CoreCallTracer.TraceError(0L, stringBuilder.ToString());
                    }
                }
                UserOptionsType userOptionsType = new UserOptionsType();
                bool            flag            = true;
                try
                {
                    userOptionsType.Load(mailboxSession, false, false);
                }
                catch (QuotaExceededException ex2)
                {
                    ExTraceGlobals.UserContextCallTracer.TraceDebug <string>(0L, "[RequestDispatcher::HandleLanguagePost] UserOptions.LoadAll failed. Exception: {0}.", ex2.Message);
                    flag = false;
                }
                userOptionsType.TimeZone = timeZoneKeyName;
                userOptionsType.IsOptimizedForAccessibility = isOptimized;
                userOptionsType.UserOptionsMigrationState   = UserOptionsMigrationState.WorkingHoursTimeZoneFixUp;
                if (flag)
                {
                    UserConfigurationPropertyDefinition propertyDefinition  = UserOptionPropertySchema.Instance.GetPropertyDefinition(UserConfigurationPropertyId.TimeZone);
                    UserConfigurationPropertyDefinition propertyDefinition2 = UserOptionPropertySchema.Instance.GetPropertyDefinition(UserConfigurationPropertyId.IsOptimizedForAccessibility);
                    UserConfigurationPropertyDefinition propertyDefinition3 = UserOptionPropertySchema.Instance.GetPropertyDefinition(UserConfigurationPropertyId.UserOptionsMigrationState);
                    userOptionsType.Commit(mailboxSession, new UserConfigurationPropertyDefinition[]
                    {
                        propertyDefinition,
                        propertyDefinition2,
                        propertyDefinition3
                    });
                }
                RequestDispatcher.InitializeFavorites(mailboxSession);
            }
            finally
            {
                if (mailboxSession != null)
                {
                    UserContextUtilities.DisconnectStoreSession(mailboxSession);
                    mailboxSession.Dispose();
                    mailboxSession = null;
                }
            }
            return(DispatchStepResult.Continue);
        }