Authentication and scheduled jobs
Whenever you execute a scheduled job manually (for example when testing) it will be run as the current user (that’s you!). However, once it executes according to its schedule it will be run in an anonymous context. If anonymous access rights aren’t enough you have to login programmatically to execute the job as a specific 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.