基于具有双重身份验证模式的预热计时器作业,我对此进行了更改:
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.Timeout = System.Threading.Timeout.Infinite;
request.Credentials = CredentialCache.DefaultNetworkCredentials;
WebResponse response = request.GetResponse();
对此:
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.Timeout = System.Threading.Timeout.Infinite;
request.Credentials = new CredentialCache
{
{ new Uri(url), "NTLM", new NetworkCredential(username, password, domain) }
};
request.Headers.Add("Authorization", GetAuthorization(username, password, domain));
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 GTB7.1 ( .NET CLR 3.5.30729)";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.CookieContainer = new CookieContainer();
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
GetAuthorization 方法如下所示:
private function GetAuthorization(string username, string password, string domain)
{
string credentials = string.Format(@"{0}\{1}:{2}", domain, username, password);
byte[] bytes = Encoding.ASCII.GetBytes(credentials);
string base64 = Convert.ToBase64String(bytes);
return = string.Concat("NTLM ", base64);
}
使用更新后的代码,我能够访问 Classic 和 Claims Based web 应用程序中的文件。