Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Run a scheduled job as a specific user in 4.62B

Vote:
 

I've read about how to run a scheduled job as a specific user in CMS 5 (http://labs.episerver.com/en/Blogs/Ted-Nyberg/Dates/112276/8/Run-a-scheduled-job-as-a-specific-EPiServer-user/), and need to know how to do this in 4.62B (.NET 2.0).

How is this done?

Jakob

#26381
Dec 03, 2008 10:47
Vote:
 

Look at this FAQ.

http://world.episerver.com/en/FAQ/Items/How-do-I-save-a-page-as-an-anonymous-user/ 

The logic you need is there (you just need to reverse it).

/Steve

#26387
Edited, Dec 03, 2008 12:47
Vote:
 

Thanks for your reply!

Unfortunately I get the following compilation error:

Keyword 'this' is not valid in a static property, static method, or static field initializer

An object reference is required for the nonstatic field, method, or property 'EPiServer.PageBase.CurrentUser.get'

 

Code Example:

public class ExportOrders : TemplatePage
{

public static string Execute()
{

System.Security.Principal.IPrincipal principal = AuthenticationProvider.Authenticate(this, "username", "password");

CurrentUser = principal as UnifiedPrincipal;

 

[...]

#26398
Dec 04, 2008 9:08
Vote:
 

From the Stacktrace (NullException on Order.Load):

 " at EPiStore.AttributeFactory.ᐁ()\r\n at EPiStore.AttributeFactory.GetAttributeSettings(PageData itemPage)\r\n at EPiStore.DataAbstraction.CartItem..ctor(Int32 pageID, Int32 quantity, Double price)\r\n at EPiStore.DataAbstraction.CartItem..ctor(Int32 pageID)\r\n at EPiStore.DataAbstraction.OrderItem..ctor(Int32 pageID)\r\n at EPiStore.DataAbstraction.Order.ᐁ(DataSet )\r\n at EPiStore.DataAbstraction.Order.ᐁ(DataSet )\r\n at EPiStore.DataAbstraction.Order.Load(Int32 id)\r\n at KnowIT.Shop.Core.ExportOrders.Execute()"

 

#26479
Dec 08, 2008 10:19
Vote:
 
You are calling Authenticate() from a static method, therefor you can not use "this" as a parameter. Try to use something else, eg "new ExportOrders()".
#26481
Dec 08, 2008 11:39
Vote:
 

Thanks for the suggestion.

Even if I make that change, I still get en error message for CurrentPage (for the same reason, since I'm using a static method):

An object reference is required for the nonstatic field, method, or property 'EPiServer.PageBase.CurrentUser.get'  

Is it possible to create an instance of the ExportOrders class (from inside the static method Execute()), and run the Scheduled Job through that?

 

 

#26483
Dec 08, 2008 14:41
Vote:
 

Try to set System.Threading.Thread.CurrentPrincipal instead of CurrentUser.

#26514
Dec 09, 2008 9:35
Vote:
 

Thanks, Erik, but I still get the same error message, and the EPiServer Support seems to have given up on this issue.

More ideas are greatly appreciated! :)

 

Jakob

#26637
Dec 15, 2008 11:17
Vote:
 

Where do you use CurrentPage here? You cannot use CurrentPage in a scheduled task. There is no request, no HttpContext, no template and no current page :-)

/steve

#26641
Dec 15, 2008 11:55
Vote:
 

Hi Steve!

I'm not using CurrentPage, I'm using the method Load in the class Order (EPiStore), which deep down is fetching properties from the product (which is a Page), and there is my problem.

Since there is no overloaded version of Order.Load, I can't edit the accesslevel.

 

Best Regards

Jakob

#26647
Dec 15, 2008 13:36
Vote:
 

Ah, hm, bummer.

And you have tried setting Thread.CurrentPrincipal to a known Principal with the needed access rights? Cause that is the one used to check for access rights.

You should be able to create a UnifiedPrincipal with an UserSid object (UserSid.Load(...)).

/steve

#26655
Dec 15, 2008 15:06
Vote:
 

Steve has the working solution for EPiServer 4, along with earlier mentioned, this works:

System.Security.Principal.IPrincipal principal = AuthenticationProvider.Authenticate(new YourClassThatYouAreInNow(), "user", "password");
Thread.CurrentPrincipal = principal as UnifiedPrincipal;
/ Jacob
#45001
Edited, Oct 26, 2010 10:31
* 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.