Share via


RunWithElevatedPriviliges is not running with elevated priviliges!!!

Here is code which works for me,

 

public interface IRemoteMethods : IDisposable
{
void createUserProfile();

}

    public class RemoteMethods : MarshalByRefObject, IRemoteMethods
{
public void createUserProfile()
{

//Code to create User Profile

        }

        public void Dispose()
{
}

  }

    public class RemoteInterfaceFactory : IDisposable
{

        private AppDomain appDomain = null;
private IRemoteMethods remoteInterface = null;

        public IRemoteMethods Loader
{
get
{
return remoteInterface;
}
} // End of property Loader

        public RemoteInterfaceFactory()
{

this.appDomain = AppDomain.CreateDomain("SecondaryAppDomain", AppDomain.CurrentDomain.Evidence);

            // Create an instance of the RemoteMethods class in the secondary domain
// and get an interface to that instance. This ensures you can use the
// instance but are not loading the instance into the primary AppDomain.
remoteInterface = (IRemoteMethods)appDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().GetName().FullName, typeof(RemoteMethods).FullName);

} // End of RemoteInterfaceFactory

        public void Dispose()
{

            if (this.remoteInterface != null)
{
this.remoteInterface.Dispose();
this.remoteInterface = null;
}

            if (this.appDomain != null)
{
AppDomain.Unload(appDomain);
this.appDomain = null;
}

        } // End of Dispose

    }
  

Folowing will go in webpart code:

SPSecurity.RunWithElevatedPrivileges(delegate()
{

                    using (RemoteInterfaceFactory remoteFactory = new RemoteInterfaceFactory())
{
remoteFactory.Loader.createUserProfile();
}

               }