How To Authenticate to a Proxy Server within a Visual Studio .WebTest using Visual Studio 2008 SP1
So you record a webtest in Visual Studio and during the playback you notice that you receive an HTTP response with the following, and all of your tests fail.
HTTP/1.1 407 Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy filter is denied. )
Proxy-Authenticate : Negotiate,Kerberos,NTLM,Basic realm="EXAMPLE.EXAMPLE.COM"
Now what do you do?
I see this question come up from time to time, so I thought I would blog about it to make things easier for folks who run into this problem.
What is the HTTP 407 response and how do I solve this?
First, your proxy is forcing you to authenticate to it *before* you are allowed to make your connections outbound from it and over to the system you want to test.
First question I would have - is Do you really have to go through the proxy? Downside is that the proxy is going to skew your test results if you are running a load test, because the proxy may breakdown or delay your responses, or it could end up caching files, and improve your responses. Either way it is a variable that if you can eliminate would be preferrable if you want reliable results in a Visual Studio LoadTest.
So, lets assume you cant get around the proxy issue.
1) You must have VS2008 Service Pack 1, there were some bugs fixed around the proxy implementation explicitly.
2) You can create a coded test or you can implement a WebTest plug-in, becuase you are going to need to explicity authenticate to the proxy, before you deal with anything else.
So, why do I need to do this in code in a plugin?
The short answer is that the WebProxy is not exposed in the User Interface within a Visual Studio .WebTest, so you will need to set the WebProxy in code. This is actually not that difficult and I have a sample plugin that you can use below to get you started.
1 | using System; |
2 | using Microsoft.VisualStudio.TestTools.WebTesting; |
3 | using Microsoft.VisualStudio.TestTools.WebTesting.Rules; |
4 | using System.Net; |
5 | |
6 | namespace WebTestPluginNamespace |
7 | { |
8 | public class MyWebTestPlugin : WebTestPlugin |
9 | { |
10 | |
11 | |
12 | public override void PreWebTest(object sender, PreWebTestEventArgs e) |
13 | { |
14 | // Create a WebProxy object for your proxy |
15 | WebProxy webProxy = new WebProxy("<https://yourproxy>"); |
16 | |
17 | //Set the WebProxy so that even local addresses use the proxy |
18 | // webProxy.BypassProxyOnLocal = false; |
19 | |
20 | // Use this WebProxy for the Web test |
21 | e.WebTest.WebProxy = webProxy; |
22 | |
23 | |
24 | |
25 | e.WebTest.PreAuthenticate = true; |
26 | NetworkCredential proxyCredentials; |
27 | |
28 | proxyCredentials = new NetworkCredential(); |
29 | |
30 | proxyCredentials.Domain = "yourDomain"; |
31 | proxyCredentials.UserName = "yourUserName"; |
32 | proxyCredentials.Password = "yourPassword"; |
33 | e.WebTest.WebProxy.Credentials = proxyCredentials; |
34 | |
35 | |
36 | |
37 | |
38 | } |
39 | } |
40 | } |
41 |
Comments
Anonymous
October 31, 2011
Ok thanks for this, is this the same in visual studio 2010 load test or can you do this via the GUI now in vs 2010?Anonymous
November 11, 2011
Hi Brad, Yes this will work for VS2010.Anonymous
November 28, 2011
This functionality is available via the designer in Visual Studio 2010 - each .webtest has these properties: Proxy User Name Password PreAuthenticateAnonymous
January 20, 2012
The Proxy, UserName and Password properties have always existed. However, its important to make the distiniction to their purpose. The Proxy property is of course the proxy where you want traffic to go outbound through. The UserName and Password properties only work for Windows Authentication such as when a web site your trying to access challenges, and of course they dont work for web forms based authentication. The UserName and Password are not used when responding to a challenge from a proxy (HTTP 407) as the proxy challenge mechanism is not handled by default, so you need the plugin to accomplish this.Anonymous
August 25, 2014
when the above plugin is used, i am getting the below error during the load test Error: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond...