Run a scheduled job as a specific EPiServer user
As noted in my post about how to Create a custom scheduled job in EPiServer all scheduled runs will execute as an anonymous user whereas jobs that are executed manually are run as the current EPiServer user.
How to log on programmatically as a specific EPiServer user
Logging on as a specific user through code is an easy task in EPiServer. In fact you do not even have to know the password of the user you want to "impersonate":
1. Import the EPiServer.Security namespace
using EPiServer.Security;
2. Log on with the specified username
PrincipalInfo.CurrentPrincipal =
PrincipalInfo.CreatePrincipal("your.username");
Any code following the previous statement will be run as if the "your.username" user was logged in.
In scheduled jobs that require specific user permissions I usually check the PrincipalInfo.CurrentPrincipal object to see if the job is executed anonymously:
if(PrincipalInfo.CurrentPrincipal.Identity.Name==string.Empty)
{
//Anonymous execution, log in programmatically
}
Update: You may also use the BypassAccessCheck parameter to disable permission checking. If you do your code will be able to modify files etc without programmatically logging in. However, I recommend logging in using a designated account when modifying content through code to make version logs easier to interpret.
18 August 2008