Try our conversational search powered by Generative AI!

Is it possible to do user impersonation outside of a scheduled job?

Vote:
 

I've used this line of code for years to impersonate a specific user inside of a scheduled job:

PrincipalInfo.CurrentPrincipal = ServiceLocator.Current.GetInstance().CreatePrincipal(userName);

However, this doesn't seem to work outside of a scheduled job. When I attempt it, I get this:

ServiceLocationProvider must be set.

I'm attempting to implement a token auth scheme, where an anonymous request bearing a token can impersonate a specific user. Do I need to manually add the IUserImpersonation service to StructureMap?

#188156
Feb 13, 2018 12:20
Vote:
 

The error message rather indicate that episerver haven't been properly initialized.

My first question is: do you get same error if you do this?

ServiceLocator.Current.GetInstance<IContentLoader>();
#188158
Feb 13, 2018 13:29
Vote:
 

@erik: Yes, that throws the same error.

#188179
Feb 13, 2018 16:30
Vote:
 

Then episerver haven't been initialized properly.

What context do you have? Are you trying this in the CMS site, the commerce site or in an application?

#188181
Feb 13, 2018 16:35
Vote:
 

In CMS, in the ProcessRequest method of a handler (implementing IRouteHandler and IHttpHandler) mapped like this:

RouteTable.Routes.Add(new Route
(
  "my/path",
  new MyHandler()
));
#188182
Feb 13, 2018 16:40
Vote:
 

HttpHandlers are indeed a very special case as much of the episerver context isn't setup that early.

I'm afraid i can't help you further, i generally avoid placing code in http handlers. embarassed

#188184
Feb 13, 2018 16:45
Vote:
 

Hey Deane

This thread might be of some help: https://world.episerver.com/forum/developer-forum/-Episerver-75-CMS/Thread-Container/2017/9/scheduled-job-elevated-permissions-not-working/

David

#188185
Feb 13, 2018 16:46
Vote:
 

That post showed me to how to do it another way, so thanks.

#188253
Feb 15, 2018 18:31
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.