SaaS CMS has officially launched! Learn more now.

Scheduled Job - how to execute a scheduled job as the current user? v12


Problem: When attempting to update a page property from inside a Scheduled Job, I get an AccessDeniedException:  EPiServer.Core.AccessDeniedException: 'Access was denied to content 1501_2939. The required access level was "Edit".'  This is due to the page property having a PropertyEditRestriction that limits changes to the CmsAdmins or Administrator roles.


Note: My user is a member of the Administrators group, so running the Job manually should let me overcome this restriction.


I’m making a Scheduled Job to be run manually.  I’d like it to run under the permissions of the current user.  However, when I run the scheduled job and interrogate the CurrentPrincipal, it seems that the job is not running as my user, but as some sort of unauthenticated ‘blank’ user.

 var user = PrincipalInfo.CurrentPrincipal;
 var i = user.Identity;
 var r = user.IsInRole(Roles.Administrators);

If I use the PrincipalInfo.CurrentPrincipal.IsInRole(Roles.Administrators), it’s false.




Edited, Aug 31, 2023 11:16

Not sure if I understand your question. Do you mean you want to run the job manually using the Admin UI? then it should be using your current user 

Aug 31, 2023 12:56

I thought so too, @quan  The executing user has changed in v12 Scheduled Jobs -

To paraphrase Tomas' excellent article:

[ScheduledPlugIn(DisplayName = "My Scheduled Job")]
public class MyJob : ScheduledJobBase
    private readonly IPrincipalAccessor _principalAccessor;

    public MyJob (IPrincipalAccessor principalAccessor)
        _principalAccessor = principalAccessor;

    public override string Execute()
        _principalAccessor.Principal = new GenericPrincipal(
            new GenericIdentity("Some Dummy User"), 
            new[] { "WebEditors" }

        // Do some suff that needs WebEditor privileges.
Aug 31, 2023 14:51
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.