title: Can I Create a Controller instance manually, then run it as a user?
body: <p>For example, from a controller I want to do something like this:</p>
  
  <pre><code>var controller = new SomeController(); //different controller
  //as of now, the controller.User is null, I want to set it
  controller.DoSomething();
  </code></pre>
  
  <p>I want to set the User as it uses the roles to decide what to do here.  I would prefer to leave all the role handling in that controller (not my code).   Is there any way to set the User?  Or possibly run it as the same user as I am calling this from another controller in the same MVC project?</p>
  
  <p>EDIT: My solution after Sergey's help, in my Controller BaseClass:</p>
  
  <pre><code>public T RunControllerAsCurrentUser&lt;T&gt;(T controller, RouteData routeData = null) where T : ControllerBase
  {
      var newContext = new ControllerContext(new HttpContextWrapper(System.Web.HttpContext.Current), routeData ?? new RouteData(), controller);
      controller.ControllerContext = newContext;
      return controller;
  }
  </code></pre>
  
  <p>Then to use it:</p>
  
  <pre><code>var someController = RunControllerAsCurrentUser(new Some.NameSpace.SomeController());
  var result = someController.SomeAction();
  </code></pre>