Kelly Lloyd

Kelly Lloyd

  • NA
  • 3
  • 0

A few questions I have about windows impersonation

Nov 29 2006 4:40 PM
I am developing a c# 2.0 windows application and am using windows impersonation to allow my application access to certain files on the network. It works fine, but when i undo the impersonation and then at some later point redo it, i no longer have access to the network resources/files (within the context of the application). It acts as if i am not impersonating.

Early in the application i call the impersonation:

 public MainForm()

{
      Thread th = new Thread(new ThreadStart(Splash));
      th.Start();
      Thread.Sleep(1000);
      InitializeComponent();
      AppConfiguration.LoadConfiguration("Application.Config.xml");
      privUser = EncDec.Decrypt(AppConfiguration.PAKey1, AppConfiguration.PAKey3);
      passWord = EncDec.Decrypt(AppConfiguration.PAKey2, AppConfiguration.PAKey3);
      iu = new ImpersonateUser();
     
iu.Impersonate(domain, privUser, passWord);
     TreeNode fs = GenerateAptTree(AppConfiguration.RootAptPath);
     aptTreeView.Nodes.Add(fs);
     aptTreeView.Nodes[0].ExpandAll();
     th.Abort();
}


Then later in the application i am required to undo impersonation because when i try to run an external application if i remain impersonated, the
external application does not run:

 private void OptimizeFile(FileStruct file)

    ..................
 
     // The usr file is updated and all the required files are placed in the temp directory. I need to
     // Kick off the optimization process
     // Undo the impersonation before running process
     iu.Undo();
     Process vericut = new Process();
     vericut.StartInfo.UseShellExecute = true;
     vericut.StartInfo.FileName = @"C:\cgtech54\windows\commands\vericut.bat";
     vericut.StartInfo.Arguments = "batch optimize \"usr=" + usrFile + "\"";
     vericut.StartInfo.WorkingDirectory = Application.StartupPath + "\\Temp\\" + dirName + "\\";
     vericut.EnableRaisingEvents = true;
     vericut.Exited += new EventHandler(Optimize_Exited);
     vericut.Start();
     iu.Impersonate(domain, privUser, passWord);
}


Notice i try to re enable the impersonation. It does not work. Is this because the impersonation is only good for the lifetime of the function i called it in?
I would not have to undo the impersonation if the Process vericut would work while impersonated. Does anyone know why the process would not work?
Is it because i am calling an application that is outside the app domain? (i.e the executable is not located in the application path?)

For debugging purposes, i tried to kick off calc.exe to test the process and when i am impersonated, the calc winodw opens then quickly closes with no error, but then i undo the impersonation before starting the process. The calculator program comes up fine.

Thanks for any help that you can give me.