Switching user context within a scheduled job

Vote:
 

Hi,

Does anyone know of a way to safely switch the context of the current user within a ScheduledJob on EPiServer 4.62?

We're using:

loginResult = EPiServer.Util.LoginBase.Login(username, password, false); 

However, this is producing a NullReferenceException.  I suspect this is being caused by a lack of HttpContext, but I don't know.

 Any  advice gratefully received!

Best Regards,

Andy

#20066
May 20, 2008 11:36
Vote:
 

Here is some code, I hope it will help 

System.Web.Security.Membership.Provider.ValidateUser(username, password);

if (isValidUser)
{
MembershipUser mu = System.Web.Security.Membership.Provider.GetUser(username, true);
EPiServer.Security.PrincipalInfo.CurrentPrincipal = EPiServer.Security.PrincipalInfo.CreatePrincipal(username);
} else
throw new Exception( "Fail on validate user " + username );

#20070
May 20, 2008 12:01
Vote:
 
#20491
Jun 02, 2008 18:10
Vote:
 

Hi Ruwen,

 

Many thanks for your reply.  Unfortunately that code only seems to work in EPiServer 5.  I've worked around the problem for now by not requiring to change the user ID at all.

 

What originally motivated the problem was that I was getting AccessDenied errors when loading and saving some pages, and there is no "NoAccess" enum value in EPiServe, however using AccessControlList.NoAccess and passing that as the AccessLevel parameter allowed me to work around the problem.

Cheers,

 

Andy

#20493
Jun 02, 2008 20:09