Download Using WebClient - IIS Basic Authentication

I use this to download html content from a site published in IIS:

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

But if basic authentication is set for IIS, this does not work. The user already enters the user and password in the IIS dialog box.

Is there any way to make this work without re-entering the user and password?

+4
source share
3 answers

Search 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 the user and password when IIS is installed, do basic authentication!

0
source

I suspect you will need a password for basic authentication, so I suspect you will need new System.Net.NetworkCredential("your-username","your-password") . By default, credentials will work with the built-in auth attribute, but not (AFAIK). Therefore, in response to:

Is there any way to make this work without re-entering the user and password?

No, I do not think so.

+4
source

I am using the following NTLM authentication code with default credentials

 webClient.Proxy = WebRequest.GetSystemWebProxy(); webClient.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; 

Once I researched a problem and found that it works. However, with .Net 4.0, my Visual Studio says GetSystemWebProxy is currently not recommended.

0
source

Source: https://habr.com/ru/post/1315402/


All Articles