RunAs A different user when debugging in Visual Studio
Asked Answered
S

8

90

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?

Shawnee answered 17/8, 2009 at 9:54 Comment(0)
S
34

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)

Shawnee answered 17/8, 2009 at 10:29 Comment(5)
How do you do this in Visual Studio? Do you open the solution, build and somehow run the above command from Visual Studio? I use VS2008.Jape
Got it, see social.msdn.microsoft.com/Forums/en/vsdebug/thread/…Jape
This method has a downside in that it won't hit any breakpoints in your code.Anaya
@LewsTherin After the project starts running, you need to go to Debug -> Attach to Process to attach the VS debugger to your program. Not ideal, but it seems to be all we have.Butterwort
@GabrielLuci That can miss some code that runs before you can get to that menu. The better option is to add the following to the first line of the entry point of your code: System.Diagnostics.Debugger.Launch();Anaya
M
91

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).

Moratorium answered 16/10, 2012 at 20:39 Comment(9)
This is by far the best solution on here. Works very well. The only hitch is that if you can't access TFS or any of your paid add-ons (like R#). I worked around this by simply reopening the solution normally (under my account).Totalitarianism
There is nothing wrong with opening the same solution from two different users like this answer suggests. You can have them open side-by-side, which is why I suggested switching your VS theme for one of them.Moratorium
I experienced an issue with this solution. The debugger entered a infinite loop when Show was invoked on a Window.Detector
I used this method for years until visual studio 2015. This version of VS wont let me sign in to obtain an MSDN key when running as a different user so it just closes. Any workarounds for that?Helmuth
@Helmuth - start your own Stack Overflow question ;-)Moratorium
Unfortunately running VS as another user doesn't play nice when the solution in under source control.Lotz
This doesn't work well with community. It asks you to log in and I don't want a login for some random user I am testing.Wariness
Does have the issue of having to wait for VS to prepare for first use if the test user hasn't previously used it. But otherwise it seems to work a treat for VS2013.Fellers
running VS as the different user might work nicely but if that user doesn't have access to some of your files, you will need to add that user as Admin on your dev box. This is not ideal.Mcfarlane
S
34

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)

Shawnee answered 17/8, 2009 at 10:29 Comment(5)
How do you do this in Visual Studio? Do you open the solution, build and somehow run the above command from Visual Studio? I use VS2008.Jape
Got it, see social.msdn.microsoft.com/Forums/en/vsdebug/thread/…Jape
This method has a downside in that it won't hit any breakpoints in your code.Anaya
@LewsTherin After the project starts running, you need to go to Debug -> Attach to Process to attach the VS debugger to your program. Not ideal, but it seems to be all we have.Butterwort
@GabrielLuci That can miss some code that runs before you can get to that menu. The better option is to add the following to the first line of the entry point of your code: System.Diagnostics.Debugger.Launch();Anaya
O
20

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

alt text

Ober answered 13/9, 2010 at 22:39 Comment(8)
I should be mentioned that you need VSCommands Pro to have this feature enabled. In the Lite (free) version, these commands seem to be available, but nothing happens when they are invoked. I tried it with VS2010 Pro.Earleanearleen
I tested this using a C# console application, which prints out the username of the user running the application. Using the option to run as different user works fine. I don't know about running as administrator or running as normal user...Greenery
@Earleanearleen I tried it with "VSCommands for Visual Studio 2013", and it works as expected. (Except that full paths are required for anything specified in "Command line arguments".)Besmear
Great solution, this extension is really nice and is free for 2012 and 2013, it adds quite a bit more useful stuff than just this.Suppository
I just checked their website and the "Debug as" feature requires paid version. The question does not specify whether nor not the solution should be free, otherwise this I would probably down vote this one.Hubing
Shame this project isn't maintained anymore.Butterwort
Project is dead... any alternatives for VS 2015?Loma
what can I use with VS 2019?Mcfarlane
R
7

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.

Residentiary answered 18/12, 2013 at 13:59 Comment(0)
S
3

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.

Secularity answered 9/9, 2011 at 1:32 Comment(0)
M
2

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.
Mcfarlane answered 30/7, 2020 at 16:37 Comment(0)
D
2

Using BAT file to run Visual Studio as a different user. Especially when running in a corporate, with different domain!

  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...

Dissimulate answered 28/3, 2022 at 7:13 Comment(1)
This is the simplest / best solution. I'm using this for a while now. What I found though is that while you can debug and develop in VS as different user it causes problems with GitHub, because it uses nonsense authentication methods, MS credentials store and OAuth. So one must open VS studio with normal credentials and then debug with VS running with different creds - if using GitHub for repo or GitHub Copilot for AI hinting.Algesia
C
-1

I'm using Visual Studio 2015 and attempting to debug a website with different credentials.

(I'm currently testing a website on a development network that has a copy of the live active directory; I can "hijack" user accounts to test permissions in a safe way)

  1. Begin debugging with your normal user, ensure you can get to http://localhost:8080 as normal etc
  2. Give the other user "Full Control" access to your normal user's home directory, ie, C:\Users\Colin
  3. Make the other user an administrator on your machine. Right click Computer > Manage > Add other user to Administrator group
  4. Run Internet Explorer as the other user (Shift + Right Click Internet Explorer, Run as different user)
  5. Go to your localhost URL in that IE window

Really convenient to do some quick testing. The Full Control access is probably overkill but I develop on an isolated network. If anyone adds notes about more specific settings I'll gladly edit this post in future.

Capers answered 1/12, 2015 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.