[英]Download using WebClient - IIS Basic Authentication
I am using this to download html content from a site published on IIS: 我用这个从IIS上发布的网站下载html内容:
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. 但是当IIS设置为基本身份验证时,这不起作用。 The user already type user and password on the IIS dialog box. 用户已在IIS对话框中键入用户和密码。
There is a way to make this work without pass a user and password again? 有一种方法可以使这项工作不再通过用户和密码?
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")
. 我怀疑基本身份验证需要密码,所以我怀疑你需要一个new System.Net.NetworkCredential("your-username","your-password")
。 The default credential would work with integrated auth, but not (AFAIK) basic. 默认凭据适用于集成的身份验证,但不适用于(AFAIK)基本身份验证。 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. 不,我不这么认为。
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! 我们可以在设置IIS时获取用户和密码进行基本身份验证!
I use the following code for the NTLM authentication with default credentials 我使用以下代码进行NTLM身份验证和默认凭据
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. 但是,使用.Net 4.0,我的Visual Studio说GetSystemWebProxy
目前已被弃用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.