Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RunAs A different user when debugging in Visual Studio

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?

like image 809
moogs Avatar asked Aug 17 '09 09:08

moogs


People also ask

How do I run an executable as a different user?

To Run a program as a different user, simply press the Shift key and right-click on the shortcut or executable you wish to Run as different user. From the right-click context menu, select Run as different user.

How do I change the run and Debug code in Visual Studio?

Once you have your launch configuration set, start your debug session with F5. Alternatively, you can run your configuration through the Command Palette (Ctrl+Shift+P) by filtering on Debug: Select and Start Debugging or typing 'debug ' and selecting the configuration you want to debug.

How do I Debug a mixed code in Visual Studio?

Select the C++ project in Solution Explorer and click the Properties icon, press Alt+Enter, or right-click and choose Properties. In the <Project> Property Pages dialog box, expand Configuration Properties, and then select Debugging. Set Debugger Type to Mixed or Auto. Select OK.

How do I run Visual Studio as administrator?

Run Visual Studio as an administrator Open the Start menu, and scroll to Visual Studio. From the right-click or context menu of Visual Studio 2019 or Visual Studio 2022, select More > Run as administrator.


6 Answers

As mentioned in have debugger run application as different user (linked above), another extremely simple way to do this which doesn't require any more tools:

  • Hold Shift + right-click to open a new instance of Visual Studio.
  • Click "Run as different user"

    Run as Different user

  • Enter credentials of the other user in the next pop-up window

  • Open the same solution you are working with

Now when you debug the solution it will be with the other user's permissions.

Hint: if you are going to run multiple instances of Visual Studio, change the theme of it (like to "dark") so you can keep track of which one is which easily).

like image 97
Watki02 Avatar answered Oct 06 '22 00:10

Watki02


This works (I feel so idiotic):

C:\Windows\System32\cmd.exe /C runas /savecred /user:OtherUser DebugTarget.Exe

The above command will ask for your password everytime, so for less frustration, you can use /savecred. You get asked only once. (but works only for Home Edition and Starter, I think)

like image 25
moogs Avatar answered Oct 06 '22 01:10

moogs


you can also use VSCommands 2010 to run as different user:

alt text

like image 38
Registered User Avatar answered Oct 06 '22 00:10

Registered User


I'm using the following method based on @Watki02's answer:

  1. Shift r-click the application to debug
  2. Run as different user
  3. Attach the debugger to the application

That way you can keep your visual studio instance as your own user whilst debugging from the other.

like image 27
noelicus Avatar answered Oct 06 '22 01:10

noelicus


cmd.exe is located in different locations in different versions of Windows. To avoid needing the location of cmd.exe, you can use the command moogs wrote without calling "cmd.exe /C".

Here's an example that worked for me:

  1. Open Command Prompt
  2. Change directory to where your application's .exe file is located.
  3. Execute the following command: runas /user:domain\username Application.exe

So the final step will look something like this in Command Prompt:

C:\Projects\MyProject\bin\Debug>runas /user:domain\username Application.exe

Note: the domain name was required in my situation.

like image 31
Michael Avatar answered Oct 05 '22 23:10

Michael


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.
like image 42
Saher Ahwal Avatar answered Oct 06 '22 00:10

Saher Ahwal