Run a scheduled job as a specific EPiServer user

by: Ted Nyberg (Hallvarsson & Halvarsson)

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


Comments

  1. Hi, Is it possible to do this in EpiServer 4?
  2. Hi, Is it possible to do this in EpiServer 4?
Post a comment    
User verification Image for user verification  
Ted Nyberg (Hallvarsson & Halvarsson)

About me

I'm a consultant with the corporate communications consultancy Hallvarsson & Halvarsson, part of the transatlantic stakeholder management and financial communications firm Sage Holdings.

My focus areas include web applications, intranets, and corporate websites.

Live Messenger

Hallvarsson & Halvarsson logo
 MCPD, MCTS and MCP logos

Bloggtoppen.se

Topplista Favoritlistan.se

Add to Technorati Favorites


Syndications


Archive


Tag cloud

EPiTrace logger