November Happy Hour will be moved to Thursday December 5th.

Magnus Rahl
Dec 15, 2010
  15182
(3 votes)

Run a scheduled job as a specific role

As you may all know, executing a scheduled job by manually triggering it and letting the scheduler trigger it at a specific time or interval is not exactly the same thing. The manual trigger will cause the task to run with the currently logged in user’s account and with access to that HttpContext. The scheduler will run as an anonymous principal and without HttpContext.

Setting the task scheduler user

Some of you probably solved the user problem, and any access rights problems that come with it, by emulating the user executing the job, maybe inspired by Ted Nyberg’s blog post. In many situations it is also possible to bypass the access check, like when passing AccessLevel.NoAccess for required access level to DataFactory.Save.

FindAllPagesWithCriteria problem

In CMS 5 R2 most DataFactory methods which return PageDatas were changed so that they don’t automatically filter by access rights. One that still does however is FindPagesWithCritera. It’s cousin, FindAllPagesWithCriteria is supposed to ignore access rights and return all matches, but because of a bug still present in CMS 6 it still does (fixed in CMS 6 R2). This problem is why I started exploring ways to give the task scheduler extended access rights.

Setting a task scheduler role

Now, if you’re in a situation like mine where users and roles are not stored in the EPiServer database but instead issued by WIF, or for some other reason you can’t use users actually existing but you still want to give your scheduled job permissions, you can emulate a role with permissions, like Administrators:

[ScheduledPlugIn(DisplayName="DummyScheduledTask")]
public class DummyScheduledTask
{
    public static string Execute()
    {
        if (HttpContext.Current == null)
        {
            PrincipalInfo.CurrentPrincipal = new GenericPrincipal(
                new GenericIdentity("Scheduled DummyTask"),
                new[] { "Administrators" });
        }
 
        // Calls made here, for example to FindAllPagesWithCritera
        // will see a user in role Administrators when executed by
        // the task scheduler.
    }
}

A few notes on this:

  • I only do this when there’s no HttpContext, meaning it is not manually started. If it is, the user starting it will be the one running the task. You could also check if the user is anonymous but in that there is at least a theoretical risk that you are giving administrator rights to a real anonymous user (like an attacker of some sort).
  • You can set any user name you like and it will appear for example in the version history if the job publishes pages. If you use different names for different jobs you’ll know who’s responsible :)
Dec 15, 2010

Comments

Henrik Molnes
Henrik Molnes Mar 3, 2014 10:24 AM

Thanks, exactly what I was looking for :)

Sep 29, 2016 07:00 PM

Very nice

I was just missing this part

Aziz Khakulov
Aziz Khakulov May 4, 2017 11:19 AM

How to run a scheduled job in Anonymous context (as it stated in documentation http://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/9/Scheduled-jobs/Scheduled-jobs/) while debugging/running the site on Visual Studio? Anyone knows?

Please login to comment.
Latest blogs
Shared optimizely cart between non-optimizley front end site

E-commerce ecosystems often demand a seamless shopping experience where users can shop across multiple sites using a single cart. Sharing a cart...

PuneetGarg | Dec 3, 2024

CMS Core 12.22.0 delisted from Nuget feed

We have decided to delist version 12.22.0 of the CMS Core packages from our Nuget feed, following the discovery of a bug that affects rendering of...

Magnus Rahl | Dec 3, 2024

Force Login to Optimizely DXP Environments using an Authorization Filter

When working with sites deployed to the Optimizely DXP, you may want to restrict access to the site in a particular environment to only authenticat...

Chris Sharp | Dec 2, 2024 | Syndicated blog

Video Guides: Image Generation Features in Optimizely

The AI Assistant for Optimizely now integrates seamlessly with Recraft AI, providing advanced image generation capabilities directly within your...

Luc Gosso (MVP) | Dec 1, 2024 | Syndicated blog