Consider a simple C # NET Framework 4.0 application that:
- uses webclient
- Authenticated using NTLM (tested on IIS 6.0 and IIS 7.5)
- extract string from URL several times using DownloadString ()
Here's a sample that works great:
using System; using System.Net; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string URL_status = "http://localhost/status"; CredentialCache myCache = new CredentialCache(); myCache.Add(new Uri(URL_status), "NTLM", new NetworkCredential("username", "password", "domain")); WebClient WebClient = new WebClient(); WebClient.Credentials = myCache; for (int i = 1; i <= 5; i++) { string Result = WebClient.DownloadString(new Uri(URL_status)); Console.WriteLine("Try " + i.ToString() + ": " + Result); } Console.Write("Done"); Console.ReadKey(); } } }
Problem:
When I enable tracing, I see that NTLM authentication is not saved.
Each time Webclient.DownloadString is called, NTLM authentication is started (the server returns the WWW-Authenticate: NTLM header and the entire authentication / authorization process is repeated, there is no "Connection: close" header).
Shouldn't NTLM authenticate the connection, not the request?
Is there a way to force WebClient to reuse an existing connection in order to avoid re-authenticating each request?
source share