title: C# SSIS Package User
body: <p>I am trying to run an SSIS package from C# which I have working by itself. The problem I am having i that I want to have the user for the package set on the fly. I set up an impersonator class that seems to be working, yet the package is throwing errors when ran from IIS. However it does work when I run it from VS2010 debug.</p>
  
  <pre><code>   using (ImpersonatedUser iu = new ImpersonatedUser("username", "domain", "password"))
     {
          Application app = new Application();
          Package package = null;
          MyEventListener eventListener = new MyEventListener();
  
          app.PackagePassword = "packagepassword";
          package = app.LoadPackage(@"packagepath", null);
  
          Microsoft.SqlServer.Dts.Runtime.DTSExecResult results = package.Execute(null, null, eventListener, null, null);
          if (results == Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure)
          {
              foreach (Microsoft.SqlServer.Dts.Runtime.DtsError local_DtsError in package.Errors)
              {
                  lblErrorOutput.Text += "&lt;br /&gt;Package Execution results: " + local_DtsError.Description.ToString();
                  lblErrorOutput.Text += eventListener.output;
              }
          }
          else
          {
              lblErrorOutput.Text = WindowsIdentity.GetCurrent().Name;
              lblErrorOutput.Text += eventListener.output;
          }
      }
  </code></pre>
  
  <p>Here is the output when running from debug then when I run from IIS (on the same computer)</p>
  
  <pre><code>domain\user
  Information: Microsoft.SqlServer.Dts.Runtime.TaskHost/SSIS.Pipeline : Validation phase is beginning.
  Information: Microsoft.SqlServer.Dts.Runtime.TaskHost/SSIS.Pipeline : Validation phase is beginning.
  Information: Microsoft.SqlServer.Dts.Runtime.TaskHost/SSIS.Pipeline : Prepare for Execute phase is beginning.
  Information: Microsoft.SqlServer.Dts.Runtime.TaskHost/SSIS.Pipeline : Pre-Execute phase is beginning.
  Information: Microsoft.SqlServer.Dts.Runtime.TaskHost/Source File [1] : The processing of file "filepathandname.csv" has started.
  Information: Microsoft.SqlServer.Dts.Runtime.TaskHost/SSIS.Pipeline : Validation phase is beginning.
  Information: Microsoft.SqlServer.Dts.Runtime.TaskHost/SSIS.Pipeline : Prepare for Execute phase is beginning.
  Information: Microsoft.SqlServer.Dts.Runtime.TaskHost/SSIS.Pipeline : Pre-Execute phase is beginning.
  Information: Microsoft.SqlServer.Dts.Runtime.TaskHost/Flat File Destination [16] : The processing of file "filepathandname.csv" has started. 
  </code></pre>
  
  <p>then when through IIS</p>
  
  <pre><code> domain\user
  Information: Microsoft.SqlServer.Dts.Runtime.TaskHost/SSIS.Pipeline : Validation phase is beginning.
  Information: Microsoft.SqlServer.Dts.Runtime.TaskHost/SSIS.Pipeline : Validation phase is beginning.
  Error in: Microsoft.SqlServer.Dts.Runtime.Package/Connection manager "SourceConnectionFlatFile" : The file name property is not valid. The file name is a device or contains invalid characters.
  Information: Microsoft.SqlServer.Dts.Runtime.TaskHost/SSIS.Pipeline : Prepare for Execute phase is beginning.
  Error in: Microsoft.SqlServer.Dts.Runtime.Package/Connection manager "SourceConnectionFlatFile" : The file name property is not valid. The file name is a device or contains invalid characters.
  Information: Microsoft.SqlServer.Dts.Runtime.TaskHost/SSIS.Pipeline : Pre-Execute phase is beginning.
  Error in: Microsoft.SqlServer.Dts.Runtime.Package/Connection manager "SourceConnectionFlatFile" : The file name property is not valid. The file name is a device or contains invalid characters.
  Information: Microsoft.SqlServer.Dts.Runtime.TaskHost/SSIS.Pipeline : Execute phase is beginning.
  Information: Microsoft.SqlServer.Dts.Runtime.TaskHost/Destination -  : The final commit for the data insertion in "component "Destination" (94)" has started.
  Error in: Microsoft.SqlServer.Dts.Runtime.TaskHost/File System Task : An error occurred with the following error message: "Could not find file 'filepathandname.csv'.". 
  </code></pre>
  
  <p>everything I see is leading me to believe that its permissions based and the user that is running the package doesnt have access to the file. When running through debug it works as it is running as me logged into the computer, however when run through IIS it is the DefaultAppPool running it.</p>
  
  <p>The check I added "lblErrorOutput.Text = WindowsIdentity.GetCurrent().Name;" is showing me the user I would expect. I can't quite figure out what is going on. Is the package execution ignoring the impersonated user? If so how do I get it to take.</p>
  
  <p>Thanks</p>