Vulnerability in EPiServer.Forms

Try our conversational search powered by Generative AI!

Jens Qvist
Dec 19, 2011
  3801
(2 votes)

Creating a BrowserLanguageCriteria for Visitor groups.

This is my first blog post ever, on any site I think! So I’m a bit nervous, but I hope that someone will find it interesting. So here goes!

This morning I decided that I should learn how to create custom criterias for Visitor groups. I started out by reading an article by Ted Nyberg, to get an idea on how it worked.

But I was short on ideas on what to build. Until I stumbled on http://criteriapack.codeplex.com/, where they had a list on planned criterias. Perfect I thought, and choose to create the BrowserLanguageCriterion. I used BrowserOSCriterion as a template for my own code, and started working.

language

So first I needed a settings class as mentioned in Teds article. I decided that the editor would want to choose a language from a dropdownlist and every culture/language in the browser should be availible. It was easy figuring out how to get all cultures. The problem for me was to get all cultures into an enum, since I started out using EnumSelectionFactory. Which I guess require an enum?

[DojoWidget(SelectionFactoryType = typeof(EnumSelectionFactory), 
        AdditionalOptions = "{ selectOnClick: true }")]
public string BrowserLanguage { get; set; }

To be honest, I haven’t used Enums much and had to google if there was a possibility to convert a list/array of languages to an enum. Apparently not, according to some forum post.

My solution was kinda easy once I figured out how to do it. I created my own SelectionFactoryType.

public class LanguageFactory : ISelectionFactory
{
    public IEnumerable<SelectListItem> GetSelectListItems(Type property)
    {
        //For each culture availible
        foreach(CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures)) {
            yield return new SelectListItem() { Text = ci.EnglishName, Value = ci.EnglishName };
        }  
    }
}
and changed my SelectionFactoryType to my newly created one;
[Required]
[DojoWidget(SelectionFactoryType = typeof(LanguageFactory))]
public string BrowserLanguage { get; set; }
Finished with my settings class, I started working on the criterion class. Basically what I needed was to compare the
browserlanguage with the language choosen in the criteria for the visitorgroup.
This was done easily by matching the two.
[VisitorGroupCriterion(
    Category = "Technical Criteria",
    Description = "Match Browser language with specified value",
    DisplayName = "Browser language")]
public class BrowserLanguageCriterion : CriterionBase<BrowserLanguageModel>
{
    private string _browserlang;
    //matches the browserlanguage with the criterion set language.
    public override bool IsMatch(System.Security.Principal.IPrincipal principal, 
System.Web.HttpContextBase httpContext)
    {
        return ((String.IsNullOrEmpty(base.Model.BrowserLanguage) ? true : 
        StringMatchHelper.IsMatch(_browserlang, base.Model.BrowserLanguage, 
        base.Model.BrowserLanguageMatchType)));             
    }

    public override void Subscribe(ICriterionEvents criterionEvents)
    {
        base.Subscribe(criterionEvents);
        criterionEvents.StartRequest += criterionEvents_StartRequest;
    }

    private void criterionEvents_StartRequest(object sender, CriterionEventArgs e)
    {
        //Get the browserlanguage
        string browserLanguage = e.HttpContext.Request.UserLanguages[0];
        Thread.CurrentThread.CurrentCulture = 
        System.Globalization.CultureInfo.CreateSpecificCulture(browserLanguage);
        _browserlang = Thread.CurrentThread.CurrentCulture.EnglishName;
    }

    public override void Unsubscribe(ICriterionEvents criterionEvents)
    {
        criterionEvents.StartRequest -= criterionEvents_StartRequest;
        base.Unsubscribe(criterionEvents);
    }
}

It was a great experience getting this to work and it made me write my first blog!
I’m quite pleased how this day turned out and hope that someone will find my
blogpost interesting!
/Jens Qvist Here is the full source;

using EPiServer.Personalization.VisitorGroups;
using EPiServer.Data;
using System.Threading;
using System.Globalization;
using EPiServer.Web.Mvc.VisitorGroups;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System;

namespace EPiServer.Classes
{
    public class BrowserLanguageModel : CriterionModelBase
    {
        private MatchStringType _browserLanguageMatchType;
    
        [DojoWidget(SelectionFactoryType = typeof(EnumSelectionFactory), 
             AdditionalOptions = "{ selectOnClick: true }")]
        public MatchStringType BrowserLanguageMatchType
        {
            get
            {
                return this._browserLanguageMatchType;
            }
            set
            {
                this._browserLanguageMatchType = value;
            }
        }
        [Required]
        [DojoWidget(SelectionFactoryType = typeof(LanguageFactory))]
        public string BrowserLanguage { get; set; }

        public override ICriterionModel Copy()
        {
            return base.ShallowCopy();
        }
    }

    [VisitorGroupCriterion(
        Category = "Technical Criteria",
        Description = "Match Browser language with specified value",
        DisplayName = "Browser language")]
    public class BrowserLanguageCriterion : CriterionBase<BrowserLanguageModel>
    {
        private string _browserlang;
        //matches the browserlanguage with the criterion set language.
        public override bool IsMatch(System.Security.Principal.IPrincipal principal, 
        System.Web.HttpContextBase httpContext)
        {
            return ((String.IsNullOrEmpty(base.Model.BrowserLanguage) ? true : 
            StringMatchHelper.IsMatch(_browserlang, base.Model.BrowserLanguage, 
            base.Model.BrowserLanguageMatchType)));             
        }
        public override void Subscribe(ICriterionEvents criterionEvents)
        {
            base.Subscribe(criterionEvents);
            criterionEvents.StartRequest += criterionEvents_StartRequest;
        }
        private void criterionEvents_StartRequest(object sender, CriterionEventArgs e)
        {
            //Get the browserlanguage
            string browserLanguage = e.HttpContext.Request.UserLanguages[0];
            Thread.CurrentThread.CurrentCulture = 
            System.Globalization.CultureInfo.CreateSpecificCulture(browserLanguage);
            _browserlang = Thread.CurrentThread.CurrentCulture.EnglishName;
        }
        public override void Unsubscribe(ICriterionEvents criterionEvents)
        {
            criterionEvents.StartRequest -= criterionEvents_StartRequest;
            base.Unsubscribe(criterionEvents);
        }
    }
    public class LanguageFactory : ISelectionFactory
    {
        public IEnumerable<SelectListItem> GetSelectListItems(Type property)
        {
            //For each culture availible
            foreach(CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures)) {
                yield return new SelectListItem() { Text = ci.EnglishName, Value = ci.EnglishName };
            }  
        }
    }
}

Dec 19, 2011

Comments

Jens Qvist
Jens Qvist Dec 19, 2011 07:30 PM

Added an image showing how the criterion looks in EPiServer.

Please login to comment.
Latest blogs
Google Read Aloud Reload Problems

Inclusive web experiences greatly benefit from accessibility features such as Google Read Aloud. This tool, which converts text into speech, enable...

Luc Gosso (MVP) | Dec 4, 2023 | Syndicated blog

Google Read Aloud Reload Problems

Inclusive web experiences greatly benefit from accessibility features such as Google Read Aloud. This tool, which converts text into speech, enable...

Luc Gosso (MVP) | Dec 4, 2023 | Syndicated blog

Import Blobs and Databases to Integration Environments

In this blog, we are going to explore some new extensions to the Deployment API in DXP Cloud Services, specifically the ability to import databases...

Elias Lundmark | Dec 4, 2023

Join the Work Smarter Webinar: Working with the Power of Configured Commerce (B2B) Customer Segmentation December 7th

Join this webinar and learn about customer segmentation – how to best utilize it, how to use personalization to differentiate segmentation and how...

Karen McDougall | Dec 1, 2023

Getting Started with Optimizely SaaS Core and Next.js Integration: Creating Content Pages

The blog post discusses the creation of additional page types with Next.js and Optimizely SaaS Core. It provides a step-by-step guide on how to...

Francisco Quintanilla | Dec 1, 2023 | Syndicated blog

Stop Managing Humans in Your CMS

Too many times, a content management system becomes a people management system. Meaning, an organization uses the CMS to manage all the information...

Deane Barker | Nov 30, 2023