【发布时间】:2016-09-30 08:51:14
【问题描述】:
考虑一个简单的 C# NET Framework 4.0 应用程序:
- 使用 WebClient
- 使用 NTLM 进行身份验证(在 IIS 6.0 和 IIS 7.5 服务器上测试)
- 使用 DownloadString() 多次从 URL 中检索字符串
这是一个运行良好的示例:
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();
}
}
}
问题:
启用跟踪时,我发现 NTLM 身份验证没有持续。
每次调用 Webclient.DownloadString 时,NTLM 身份验证开始(服务器返回“WWW-Authenticate: NTLM”标头,整个身份验证/授权过程重复;没有“Connection: close”标头)。
NTLM 不应该对连接而不是请求进行身份验证吗?
有没有办法让 WebClient 重用现有的连接以避免重新验证每个请求?
【问题讨论】:
标签: c# .net authentication webclient ntlm