1

I'm trying to navigate between sites but current language is not consistent between sites under the same tenant, how to unify lang cookie?

1 Answer 1

1

Are the sites on different domains? If so, I suspect it's a cookie issue. By default, Sitecore (and ASP.NET) will use a very specific domain for its cookies to limit the cookies to only that domain. Since cookies cannot be shared across domains, when you switch domains, Sitecore can't see the language cookie anymore.

To resolve this, I would recommend creating an httpRequestEnd pipeline processor that looks for your site's cookie (sitename#lang) in the Response.Cookies collection and updates the Domain on it to be less specific.

Disclaimer: this code has not been tested in this exact form.

The pipeline processor

namespace Custom.Pipelines.HttpRequestEnd
{
    public class UpdateLanguageCookieDomain : HttpRequestProcessor
    {
        public override void Process(HttpRequestArgs args)
        {
            // For performance and cleanliness reasons, only run this on requests for your site
            SiteContext currentSite = Context.Site;
            if (currentSite == null || !currentSite.Name.Equals("yoursitename"))
                return;

            if (string.IsNullOrEmpty(args.Context.Request.Url?.DnsSafeHost))
                return;

            string[] domainChunks = _HttpContext.Request.Url.DnsSafeHost.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            // If it's only 2 or fewer chunks, then it's safe to leave the cookie domain as the full domain
            if (domainChunks.Length > 2)
            {
                // If it's more than 2 chunks, we should set the cookie domain to the least-restrictive domain possible
                // e.g. subdomain.domain.com -> [subdomain, domain, com] -> ".domain.com"
                var domain = $".{domainChunks[domainChunks.Length - 2]}.{domainChunks[domainChunks.Length - 1]}";

                foreach (string key in args.Context.Request.Cookies.AllKeys.Where(key => key == "yoursitename#lang))
                {
                    args.Context.Response.Cookies[key].Domain = domain;
                }
            }
        }
    }
}

The config patch

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
    <sitecore>
        <pipelines>
            <httpRequestEnd>
                <processor patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.EndDiagnostics, Sitecore.Kernel']" type="Custom.Pipelines.HttpRequestEnd.UpdateLanguageCookieDomain,Custom" />
            </httpRequestEnd>
        </pipelines>
    </sitecore>
</configuration>
2
  • I'm facing the same issue again with the same domain, two sites and same domain but each is having a separate lang cookie, so by redirecting from one site to another, redirection goes to the site default language and not the language of the site I'm redirecting from, does this has another approach? Nov 13, 2018 at 11:59
  • Sitecore creates a language cookie for each site, so it's not using the language from the "old" site because it's looking for a different one. You could solve this two ways: 1) write something to modify the other site's language cookie when setting the language on one site, or 2) store the language in your own cookie and add a custom HttpRequestBeing pipeline processor to change the Sitecore.Context.Language based on that value instead of relying on Sitecore's default language resolving. Nov 13, 2018 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.