Fabio
Fabio

Reputation: 470

Download using WebClient - IIS Basic Authentication

I am using this to download html content from a site published on IIS:

using (var client = new WebClient())
{
   client.Credentials =  CredentialCache.DefaultCredentials;
   string html = client.DownloadString("http://site.com");
}

But when the IIS is set to Basic Authentication this doesn't works. The user already type user and password on the IIS dialog box.

There is a way to make this work without pass a user and password again?

Upvotes: 4

Views: 4786

Answers (3)

bohdan_trotsenko
bohdan_trotsenko

Reputation: 5357

I use the following code for the NTLM authentication with default credentials

webClient.Proxy = WebRequest.GetSystemWebProxy();

webClient.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;

One day I researched the problem and found this working. However, with .Net 4.0 my Visual Studio says GetSystemWebProxy is currently deprecated.

Upvotes: 0

Fabio
Fabio

Reputation: 470

Searching more, I found this:

HttpApplication context = (HttpApplication)HttpContext.Current.ApplicationInstance;
            string authHeader = context.Request.Headers["Authorization"];
            string userNameAndPassword = Encoding.Default.GetString(
                       Convert.FromBase64String(authHeader.Substring(6)));
            string[] parts = userNameAndPassword.Split(':');

            Response.Write(userNameAndPassword);

We can get user and password when the IIS is set do basic authentication!

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062780

I suspect that basic authentication is going to need the password, so I suspect you'll need a new System.Net.NetworkCredential("your-username","your-password"). The default credential would work with integrated auth, but not (AFAIK) basic. So in answer to:

There is a way to make this work without pass a user and password again?

No, I don't think so.

Upvotes: 4

Related Questions