Session handling in audience criteria
Control how CMS stores visitor state data for audience criteria using sessions, cookies, or custom IStateStorage implementations.
Audience criteria session handling controls how CMS stores visitor state data for personalization. The audience system autodetects whether ASP.NET session state is enabled and falls back to cookies when sessions are turned off. Custom session storage supports alternative mechanisms such as in-memory or distributed caches.
Start a visitor session
The RaiseStartSession event signals the start of a visitor session to audience criteria. When sessions are enabled, the ASP.NET session start event raises RaiseStartSession. When sessions are disabled, the HttpApplication.AcquireRequestState event raises RaiseStartSession instead.
Turn session state on and off
The session-based audience criterion stores its state in one of three locations: an HTTP session object, a cookie, or a custom state storage implementation. When sessions are enabled and no custom storage is registered, CMS uses the HTTP session object. When sessions are disabled and no custom storage is registered, CMS uses an HTTP cookie.
The HTTP session object is the default when the ASP.NET session state mode is not Off. Override this behavior by setting the EnableSession property to false on VisitorGroupOptions.
Customize state storage
Custom state storage replaces the default session or cookie storage with a custom implementation. Implement the IStateStorage interface and register it in the dependency injection container.
The following sample code shows a custom in-memory state storage implementation:
internal class InMemoryStateStorage: IStateStorage {
IDictionary<string, string> _states = new Dictionary<string, string>();
public bool IsAvailable => true;
public object Load(string key) {
_states.TryGetValue(key, out string value);
return value;
}
public void Save(string key, object value) {
_states[key] = (string) value;
}
public void Delete(string key) {
_states.Remove(key);
}
}Register the custom state storage in IServiceCollection in the startup class:
public class Startup {
public void ConfigureServices(IServiceCollection services) {
services.Add<VisitorGroupOptions>((s) => new VisitorGroupOptions(), ServiceInstanceScope.Singleton);
services.Configure<VisitorGroupOptions> (s => s.EnableSession = EnableSession);
services.Add<IStateStorage>(s => new InMemoryStateStorage(), ServiceInstanceScope.Singleton);
}
}Updated 17 days ago
