Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用WebClient下载-IIS基本身份验证_C#_Asp.net - Fatal编程技术网

C# 使用WebClient下载-IIS基本身份验证

C# 使用WebClient下载-IIS基本身份验证,c#,asp.net,C#,Asp.net,我使用此功能从IIS上发布的网站下载html内容: using (var client = new WebClient()) { client.Credentials = CredentialCache.DefaultCredentials; string html = client.DownloadString("http://site.com"); } 但是,当IIS设置为基本身份验证时,这不起作用。用户已在IIS对话框中键入用户和密码 有一种方法可以在不再次传递用户和密码的

我使用此功能从IIS上发布的网站下载html内容:

using (var client = new WebClient())
{
   client.Credentials =  CredentialCache.DefaultCredentials;
   string html = client.DownloadString("http://site.com");
}
但是,当IIS设置为基本身份验证时,这不起作用。用户已在IIS对话框中键入用户和密码


有一种方法可以在不再次传递用户和密码的情况下实现此功能?

我怀疑基本身份验证需要密码,因此我怀疑您需要一个
新的System.Net.NetworkCredential(“您的用户名”、“您的密码”)
。默认凭证可以与集成身份验证一起使用,但不能与(AFAIK)basic一起使用。因此,作为对以下问题的回答:

有没有一种方法可以在不再次传递用户和密码的情况下使此工作正常


不,我不这么认为。

搜索更多信息,我发现:

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);

当IIS设置为进行基本身份验证时,我们可以获取用户和密码

我使用以下代码对具有默认凭据的NTLM身份验证进行验证

webClient.Proxy = WebRequest.GetSystemWebProxy();

webClient.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
有一天,我研究了这个问题,发现这是可行的。然而,对于.NET4.0,我的VisualStudio说,
GetSystemWebProxy
目前已被弃用