Here is some code, I hope it will help
MembershipUser mu = System.Web.Security.Membership.Provider.GetUser(username, true);
EPiServer.Security.PrincipalInfo.CurrentPrincipal = EPiServer.Security.PrincipalInfo.CreatePrincipal(username);
throw new Exception( "Fail on validate user " + username );
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.
Does anyone know of a way to safely switch the context of the current user within a ScheduledJob on EPiServer 4.62?
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!