今天我一直拉着我的頭髮與這個問題。我一直在開發一個內部應用程序,它執行一系列HTTP GET和POST以填充一系列Web表單。當我運行fiddler2時我的代碼工作正常 - 我用來調試我的GET URIs和POST FormData的工具。現在我沒有運行fiddler2我得到一個身份驗證401錯誤。我會查看標題進行比較,但是如果不能運行提琴手就有點困難。NTLM v1認證只適用於Fiddler2不是沒有
基本上我的代碼通過訪問URI並存儲cookie來工作。訪問該網站由SSO控制,並且服務器在2003年運行時,它希望使用NTLMv1。我在Windows 7客戶端遇到的第一個問題是Win7會協商128位,而服務器只會說64位,認證將失敗(最終401)。使用fiddler2並將本地計算機上的組策略設置爲64位,我就可以完成我的工作。然後,我把這個軟件變成了網絡服務,今天發現有一個問題在於它失敗了。正如我之前所說的,它在fiddler2運行正常的情況下工作正常,給我留下了一些漏洞,因爲我無法讓每個客戶端都安裝並使用fiddler2來獲得我的功能!
首先,我有我的功能來存儲Cookie ...。然後我有另一個功能,使用該cookie執行獲取第一個功能總是失敗,「遠程服務器返回錯誤:(401)未經授權。」
我希望某處我已經錯過了某些明顯的東西,我不想做一些不可能的事情。
感謝, 鋁
/// <summary>
/// Function to get a cookie from a site providing the given site and credentials - this cookie then can be reused for subsequent calls
/// </summary>
/// <param name="credential">The NetworkCredential to access the site</param>
/// <param name="Uri">The Uri of the site</param>
/// <returns>A CookieContainer containing all needed cookies</returns>
private CookieContainer GetCookie(NetworkCredential credential, string Uri)
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(Uri);
HttpWebResponse resp;
CookieContainer cookieJar = new CookieContainer();
req.AllowAutoRedirect = true;
req.Credentials = credential;
req.CookieContainer = cookieJar;
resp = (HttpWebResponse)req.GetResponse(); // This line always fails with: The remote server returned an error: (401) Unauthorized.
return cookieJar;
}
/// <summary>
/// Function to perform a HTTP GET
/// </summary>
/// <param name="cookieJar">A CookieContainer for keeping the reference of our sessions</param>
/// <param name="credential">The Credentials to use to access the site</param>
/// <param name="Uri">The Uri to GET</param>
private void DoGet(CookieContainer cookieJar, NetworkCredential credential, string Uri)
{
HttpWebRequest req;
HttpWebResponse resp;
// Just grab the site uri where the cookie is stored
string[] UriParts = Uri.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
Uri CookieUri = new Uri(UriParts[0] + "//" + UriParts[1]);
// Use cookie information to get first page of call entry
req = (HttpWebRequest)HttpWebRequest.Create(Uri);
req.CookieContainer = new CookieContainer();
req.CookieContainer.Add(cookieJar.GetCookies(CookieUri)[0]);
req.AllowAutoRedirect = true;
req.Credentials = credential;
req.CookieContainer = cookieJar;
resp = (HttpWebResponse)req.GetResponse();
}
您是否嘗試過使用Wireshark運行?它將充當數據包捕獲,但不*代理。它應該讓你看到NTLM協商而不干擾它。 (它也做了很好的解碼NTLMSSP頭文件。) –