RunAs A different user when debugging in Visual Studio

Viewed 113204

I'm trying to run the program I'm debugging as a different user. Now, this can be done by running the exe and attaching from Visual Studio, but this is cumbersome.

What I've tried to do is use the "RunAs" command:

command.com /C runas /env /user:OtherUser DebugTarget.Exe 

But this is attached to command.com, Visual Studio wants an exe. Now I can create a dummy app....but anyone have a better solution for this?

8 Answers

You can open your command prompt as the intended user:

  • Shift + Right Click on Command Prompt icon on task bar.
  • Select (Run as differnt user)

enter image description here

  • You will be prompted with login and password

  • Once CommandP Prompt starts you can double check which user you are running as by the command whoami.

  • Now you can change directory to your project and run

dotnet run

  • In Visual Studio hit Ctrl+Alt+P (Attach to Process - can also be found from Debug menu)

enter image description here

  • Make sure "Show Processes from All users" is checked.
  • Find the running process and attach debugger.

Using BAT file to run Visual Studio as a different user.

  1. Create one .bat file.. eg: Create VS.bat in the desktop. (when you give extention as .bat, please make sure you changed the .txt or other extension at the end. Some time it wont display depending on your view settings)
  2. Right click the newly created .bat file and click "Edit". (You can edit it using Notepad or Notepad++ etc.)
  3. Copy paste the below code

runas /netonly /user:<domainName>\<user name> "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\devenv.exe"

  1. Replace the and with the values. If Domain name is not required, only use the User name with out the angle brackes "<>"
  2. Double click the VS.bat file and it will prompt you to enter the Password !

Enjoy...

Related