0

I am trying to open some locally installed applications from my home computer but run them using my work Windows account. I need to run them using my work account because they authenticate with Windows Authentication and there are natural restrictions using my local home Windows user.

For example, I can successfully run SSMS using my work account if I run this command:

runas /netonly /noprofile /user:MYWORKDOMAIN\MYWORKUSERNAME "C:\Program Files (x86)\Microsoft SQL Server Management Studio 18\Common7\IDE\ssms.exe"

My home computer is connected via a VPN but otherwise it is not under my work domain. The above command NEEDS the /netonly option to work.

However, I have a number of other apps that need this functionality and I am trying to write a small C# app that can run these apps without needing to enter my password each time. I have tried using the below snippet without success - I get an error saying the username and password are incorrect (they aren't).

var process = new System.Diagnostics.Process
{
    StartInfo =
    {
        Domain = "MYWORKDOMAIN",
        FileName = AppPath,
        Password = password,
        UserName = username,
        UseShellExecute = false,
        WindowStyle = ProcessWindowStyle.Normal,
    }
};

process.Start();

Can someone tell me what I'm missing please?

EDIT 1 - results for nickpeq's answer

After implementing the code from the provided link, I used it like this:

Console.WriteLine("Before impersonation: " + WindowsIdentity.GetCurrent().Name);

using (new Impersonator("MYWORKUSERNAME", "MYWORKDOMAIN", "MYWORKPASSWORD"))
{
    Console.WriteLine("During impersonation: " + WindowsIdentity.GetCurrent().Name);
}

Console.WriteLine("After impersonation: " + WindowsIdentity.GetCurrent().Name);

And these were the results:

Before impersonation: DESKTOP-WIN11\mark
During impersonation: DESKTOP-WIN11\mark
After impersonation: DESKTOP-WIN11\mark

There was no error thrown (so I guess that's progress )

Mark
  • 1,115
  • 1
  • 7
  • 13

1 Answers1

0

Check this answer: https://stackoverflow.com/a/11672293/7292986

I see many unanswered StackOverflow threads trying to do the same thing, but it looks like this answer may actually achieve what you are looking for (albeit with significant complexity).

nickpeq
  • 3
  • 1
  • Hey! Thanks for the response. Even after hours of googling, I didn't find that answer. However, that didn't help me :( It was an old answer and that code only worked with the old .NET Framework, but even then the impersonation didn't work. I'll update my answer in a minute and show you the results. – Mark Apr 20 '22 at 22:46