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

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

Vote:
 

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.

 

https://docs.developers.optimizely.com/content-management-system/docs/scheduled-jobs

 

Cheers,

Lance

#307734
Edited, Aug 31, 2023 11:16
Vote:
 

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 

#307746
Aug 31, 2023 12:56
Vote:
 

I thought so too, @quan  The executing user has changed in v12 Scheduled Jobs - https://www.gulla.net/en/blog/scheduled-jobs-in-optimizely-cms-12/

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.
    }
}
#307753
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.