How to Restrict Access to Sitecore SXA Site with Identity Server

Let’s imagine that you need to restrict to secure your whole site, or only your Content Management instance. No anonymous access. As a Sitecore old school guy I tough about site requireLogin setting set to true and loginPage to /sitecore/login

From old Sitecore Developer Network

 

It changes with Sitecore 9.1 and Identity Server . A must read article, Understanding Sitecore authentication behavior changes,  says that the new format is  /identity/login/{your_sxa_site_name}/SitecoreIdentityServer. 

Continue reading

Sitecore SXA Personalization based on current environment

How many times you need a different content on different environment? For example, GTM script with different params on PROD and non-PROD environment? Why not leverage SXA Multisite Environment variable and Sitecore rules?

Perso

There are 3 steps.

First. In your web.config define local environment variable

<add key=”localenv:define” value=”local” /> i.e. different value for each environment : local, dev, qa, etc.

Second.  Set SXA setting XA.Foundation.Multisite.Environment on each environment

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:localenv="http://www.sitecore.net/xmlconfig/localenv/">
<sitecore >
<settings>
<setting name="XA.Foundation.Multisite.Environment" value="LOCAL" />
<setting name="XA.Foundation.Multisite.Environment" role:require="ContentManagement" localenv:require="development">
<patch:attribute name="value">DEV-CM</patch:attribute >
</setting>
<setting name="XA.Foundation.Multisite.Environment" role:require="ContentDelivery" localenv:require="development">
<patch:attribute name="value">DEV-CD</patch:attribute>
</setting>
<setting name="XA.Foundation.Multisite.Environment" role:require="ContentManagement" localenv:require="qa">
<patch:attribute name="value">QA-CM</patch:attribute>
</setting>
<setting name="XA.Foundation.Multisite.Environment" role:require="ContentDelivery" localenv:require="qa">
<patch:attribute name="value">QA-CD</patch:attribute>
</setting>
</settings>
</sitecore>
</configuration>

 

Third. Create a rule

using Microsoft.Extensions.DependencyInjection;
using Sitecore.DependencyInjection;
using Sitecore.Diagnostics;
using Sitecore.Rules;
using Sitecore.Rules.Conditions;
using Sitecore.XA.Foundation.Abstractions.Configuration;
using Sitecore.XA.Foundation.Multisite;

namespace OneWeb.Feature.Multisite.Conditions
{
 public class SxaEnvironmentRule<T>:StringOperatorCondition<T> where T : RuleContext
 {
  public string querystringvalue { get; set; }

  protected override bool Execute(T ruleContext)
  {
   Assert.ArgumentNotNull((object)ruleContext, nameof(ruleContext));
   if (string.IsNullOrEmpty(this.querystringvalue))
   {
    return false;
   }

   var multisiteConfigurationReader = ServiceLocator.ServiceProvider.GetService<IConfiguration<MultisiteConfiguration>>();
   var multisiteConfiguration = multisiteConfigurationReader?.GetConfiguration();

   if (multisiteConfiguration == null)
   {
    return false;
   }
   return this.Compare(multisiteConfiguration.MultisiteEnvironment, this.querystringvalue);
  }
 }
}