How to install Visual Studio 2021 Preview Behind Corporate Proxy

So you are tempted to get your hands on the shiny Visual Studio 2021 Preview, right? But the evil corporate proxy is once again blocking you from expanding your curiosity? Well, fear no more. Stay with me while I’ll walk you through this in literally just a minute!

  1. Run Fiddler and make sure that fiddler is pointing to your corporate proxy as its gateway.
    1. Select Tools > Options
    2. In the Gateway tab, make sure “Use System Proxy (recommended)” is selected.
    3. Make sure Fiddler is taking care of NTLM authentication, by selecting Rules > Automatically Authenticate.

2. Download Visual Studio 2021 Preview setup from the official website.

3. Run the downloaded executable. You will be prompted to enter credentials for a local Administrator. Don’t enter your credentials yet.

4. Open the following path in your File Explorer: %USERPROFILE%\AppData\Local\Temp\1

5. Look for a folder that contains another folder called “vs_bootstrapper_d15“. The folder has had a 20 character hexadecimal name in my case (i.e. f9f02c848a65f81ccd57), but I’m pretty sure it is a random name.

6. Look for a file named “vs_setup_bootstrapper.exe.config” and open it in your text editor of choice and add the following snippet to the file and save.

<configuration>
...  
  <system.net>
    <defaultProxy>
      <proxy proxyaddress="http://localhost:8888" bypassonlocal="False" />
    </defaultProxy>
  </system.net>
</configuration>

7. Go back to the credential prompt, enter your credentials and hit OK. You are good to go!

How to install Visual Studio 2021 Preview Behind Corporate Proxy

How to fix proxy authentication issues in your development environment

I have written about proxy authentication before and although this annoying problem has been around, many tools still don’t support it well. Specially when it comes to NTLM flavor of it that has been developed by Microsoft long time ago and even many Microsoft tools did not support it until recently.

Proxy servers in my opinion are not the best tools to control internet access. Security benefit they bring us are not much compared to productivity they take away from developers.

In the past I have used many different techniques to overcome this issue (e.g. CNTLM, custom local proxy and more) but the best way that almost always works reliably and securely is to run Fiddler with its “Capture Traffic” turned off (unless you need it for other reasons) and point from your blocked app to it. This way:

  1. Fiddler won’t be capturing all the traffic and it will only proxy the traffic for the applications that are specifically pointing to it.
  2. It takes care of authentication on behalf of you without worrying to store your credentials somewhere that is not safe or having to update your credentials when they change.
  3. You can always close Fiddler when you want to cut the traffic

In the rest of this blog post I will show how you can configure every development tool that I know (and remember) to point to Fiddler as a proxy.

Keep in mind that it doesn’t have to be Fiddler, any other local proxy that can take care of authentication on behalf of the logged in user can do the same.

Please let me know if I have forgotten a developer tool or if you know how to configure another tool and what others to know it. I will add it to the list as soon as possible.

Nuget Package Manager

At this moment Nuget Package Manager does not understand proxy authentication and you need to change its configuration to use your local proxy server (Fiddler) to take care of authentication. The configuration file is located in

%USERPROFILE%\AppData\Roaming\NuGet\NuGet.Config

<configuration>
  <config>
    <add key="http_proxy" value="http://localhost:8888" />
  </config>
</configuration>

Node Package Manager (NPM)

At this moment Nuget Package Manager does not understand proxy authentication and you need to change its configuration to use your local proxy server (Fiddler) to take care of authentication.Run the following command to know if your NPM is using any proxy.

Run the following command to know your current proxy setting (if any).

npm config get proxy

To set Fiddler as your proxy setting use the following command.

npm config set proxy http://localhost:8888

You have the possibility to point to your corporate proxy server using the following syntax.

npm config set proxy http://{domain\username-url-encoded}:{password-url-encoded}@{proxy-domain-name}:{port}

But keep in mind that if you directly point to your corporate proxy, every time your password changes you need to run the above command again to update the setting.

Side: To URL encode your username or password you can use encodeURIComponent in your browser’s Console. For example:

>> encodeURIComponent('domain\\username')
"domain%5Cusername"
>> encodeURIComponent("P@ssw0rd!")
"P%40ssw0rd!"

Visual Studio’s Task Runner (NPM in Visual Studio)

Whatever I explained about configuring NPM’s proxy setting previously is still applicable here, but you need to keep in mind that Visual Studio uses its own local NPM and not the one you might have installed globally. This means that you need to run the above commands in “Developer Command Prompt for VS” and not in a standard command prompt.

Visual Studio Code

VS Code in its more recent versions supports proxy authentication and it can even help its extensions to use its proxy settings, but some extensions (e.g. NPM) still resist it (pun intended) and you might need to configure them independently. If for any reason you need to manualy set VS Code’s proxy please refer to: https://code.visualstudio.com/docs/setup/network

Git

Starting from version 1.7.10, Git supports NTLM proxy authentication. You can find the commit here. Although you still need to instruct Git to use your proxy, because it does not detect it from your OS. To do so, you can point to your proxy server’s URL and Git will do the rest. The following bash command is setting the proxy URL to Fiddler’s default URL, but you can use your company’s proxy server too. You can either point to Fiddler to take care of proxy authentication using the following command.

$ git config --global http.proxy http://localhost:8888

Or directly point to your corporate proxy and include your username. Git can remember your credentials using Windows Credential Manager, but you need to make sure that your Git client is configured to use the credential manager and you are using v. 2.8.0 or higher.

$ git config --global http.https://github.axa.com.proxy http://[proxyuser@]<proxyhost>:<port>

To make sure Git is using Windows Credential Manager run the following command.

$ git config --global credential.helper wincred
How to fix proxy authentication issues in your development environment

A shorter way to initialize Lists in C#

Threre is a trick that I wanted to show you today. A trick you can do initialize your lists a bit easier. Of course there’s some plumbing to be done.

Let’s assume you have a class definition like the following.

public class Foo
{
    public int A { get; set; }
    public int B { get; set; }
    public int C { get; set; }
    void Foo(int a, int b, int c)
    {
        A = a;
        B = b;
        C = c;
    }
}

You would typically initialize a list of Foo type like the following.

var foos = new List<Foo>();
foos.Add(new Foo(1, 2, 3));
foos.Add(new Foo(4, 5, 6));
// and more ...

But if you write and extension method like below.

public static class FooExtensions
{
    public static void Add(this IList<Foo>  list, int a, int b, int c) => list.Add(new Foo(a, b, c));
}

You can initialize your list like so:

var foos = new List<Foo>
{
    {1, 2, 3},
    {3, 4, 5},
    {6, 7, 8},
    // and more ...
};
A shorter way to initialize Lists in C#

How to use Ctrl+W to close a tab in Visual Studio

All the browsers I know give you the option to close tabs using Ctrl+W in addition to the standard Ctrl+F4 in Windows. I find Ctrl+W easier to reach than Ctrl+F4, but as a developer I switch back and forth between Visual Studio (or VS Code) and my browsers and sometimes by mistake I press Ctrl+W in Visual Studio. That’s why I looked around in Tools > Options to see if there is a way to change this shortcut in Visual Studio once and for all. It was right there waiting for me.

Here is the way to do it:

  1. Select “Tools > Options” from the menu.
  2. In the “Options” window look for “Keyboard” under “Environment“.
  3. In “Show commands containing:” textbox type “Window.CloseDocumentWindow“.
  4. There’s no surprise that the command is assigned “Ctrl+F4” by default. You need to remove it by clicking “Remove” button in front of “Shortcuts for selected command”. You might need to press a few times if there are more shortcuts assigned to it.
  5. Make sure “Global” is selected for “Use new shortcut in:“.
  6. Press “Ctrl+W” in “Press shortcut keys:” box.
  7. Don’t forget to click “OK” at the end to save your changes.

At the end, you should have something like the following picture.

Visual Studio Options - Keyboard shortcut for Windows.CloseDocumentWindow is set to Ctrl_W (Global)
Visual Studio – Options – Keyboard
How to use Ctrl+W to close a tab in Visual Studio

How to use PowerShell Invoke-WebRequest behind corporate proxy

Corportate proxies are one of the productivity killers for developers. They are not well supported in every utility and framework and each tool has its own litrature to set proxy settings. To add salt to the injury, not every tool supports NTLM authentication well which is quite common in many proxies. Companies have to sometimes make exception rules in proxy settings that can further complexify matters.

In case of PowerShell you do not have to worry much. Let’s see how you can set proxy for Invoke-WebRequest for example. Other commands usually support proxy settings similarly.

$pass = ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList "contoso\george", $pass
Invoke-WebRequest https://registry.npmjs.org/express -Proxy "http://proxy.contoso.com:8080" -ProxyCredential $cred

In line 1, we store the password in a SecureString object. In Line 2, we create a new PSCredentual object by providing the username and password and finally in Line 3, we call Invoke-WebRequest using -Proxy and -ProxyCrendential parameters.

Let me give you another alternative. Did you know you can also ask the proxy settings required for a URL from your OS and even use the current user’s credentials?

$dest = "https://registry.npmjs.org/express"
$proxy = ([System.Net.WebRequest]::GetSystemWebproxy()).GetProxy($dest)
Invoke-WebRequest https://registry.npmjs.org/express -Proxy $proxy -ProxyUseDefaultCredentials
How to use PowerShell Invoke-WebRequest behind corporate proxy