Try our conversational search powered by Generative AI!

Magnus Rahl
Dec 15, 2010
  14023
(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
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog