 * @param tenant The tenant signed in too.
     * @param sessionStorageKey The bootstrap state, session storage key.
     * @param state The bootstrap state.
     * @param redirectUri The redirect uri.
     * @param preferedPortalUri The prefered portal uri.
     */
    function setBootstrapStateAndRedirect(tenant, sessionStorageKey, state, redirectUri, preferedPortalUri) {
        var global = window;
        var location = global.location;
        var localStorage = global.localStorage;
        var sessionStorage = global.sessionStorage;
        var uriComponents = JSON.parse(localStorage.getItem(uriComponentsKey) || "{}");
        var restoredSearch = uriComponents.search || "";
        var restoredHash = uriComponents.hash || "";
        localStorage.removeItem(uriComponentsKey);
        tenant = tenant || "";
        // Check to see if prefered portal is passed in, and redirect unless the opt-out flag is present
        if (preferedPortalUri && queryExistenceCheck(restoredSearch, "feature.customportal=false") === false) {
            var preferredPortal = localStorage.getItem(preferredPortalKey);
            // If there are no preferred portal settings save them for next time.
            if (!preferredPortal) {
                var now = new Date();
                localStorage.setItem(preferredPortalKey, JSON.stringify({
                    expiresOn: now.getTime() + preferredPortalExpiryOffset,
                    uri: preferedPortalUri
                }));
            }
            redirect(preferedPortalUri + tenant + restoredSearch + restoredHash);
        }
        else {
            // Our sign-in flow is done, remove the hash and search we started with
            // Otherwise a new sign-in flow in this session will not be able to save the hash and search it started with
            if (state) {
                state.hash = restoredHash;
                state.createdOn = Date.now();
            }
            sessionStorage.setItem(sessionStorageKey, JSON.stringify(state));
            if (restoredSearch && redirectUri.indexOf("?") !== -1) {
                restoredSearch = "&" + restoredSearch.substr(1);
            }
            // Because this script is rendered in response to a POST we want to redirect to a GET (to prevent an F5 from prompting)
            // We also dont want pressing the back button to prompt to repost.
            // So we use location.replace to prevent the POST from being added to navigation history.
            location.replace(redire