London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Magnus Rahl
Dec 15, 2010
  15737
(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

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
Notes on Optimizely Self-Optimizing Block

While the free A/B Testing might be dead , the Self-Optimizing Block is still alive and kicking. Here's some notes for those debugging it.  ...

Jacob Pretorius | Apr 29, 2025

Optimizely Product Recommendation Troubleshooting

In today’s fast-paced digital landscape, personalization is everything . Customers expect relevant, tailored experiences whenever they interact wit...

Sanjay Kumar | Apr 28, 2025

Natural Language Q&A in Optimizely CMS Using Azure OpenAI and AI Search

In Part 2, we integrated Azure AI Search with Azure Personalizer to build a smarter, user-focused experience in Optimizely CMS. We used ServiceAPI ...

Naveed Ul-Haq | Apr 25, 2025 |

Identifying Spike Requests and Issues in Application Insights

Sometimes within the DXP we see specific Azure App Instances having request spikes causing performance degredation and we need to investigate. I fi...

Scott Reed | Apr 25, 2025