8
運行下面的代碼:傳遞用戶名和密碼通過URI .NET的HttpWebRequest不起作用
var request = HttpWebRequest.Create("http://username:[email protected]/test-http-status-codes.asp?code=401");
var response = request.GetResponse();
...並檢查使用Wireshark的請求表明,沒有授權我的客戶端嘗試(網址爲一個簡單的服務將永遠返回401)。
此代碼的初始攻擊後發送一個授權報頭:
var request = HttpWebRequest.Create("http://username:[email protected]/test-http-status-codes.asp?code=401");
request.Credentials = new NetworkCredential("username", "password");
var response = request.GetResponse();
使用System.Uri
類沒有效果。爲什麼在URL中傳遞的用戶名和密碼不用於身份驗證?
(我是經過授權頭沒有初始的挑戰意識到this blog post,而不是眼前的問題)
編輯我要補充,這是很容易解決這個限制,例如與該位的代碼(添加URL未逃逸的味道),我只是好奇,爲什麼你要做到這一點:
var userInfo = request.Address.UserInfo;
if (!string.IsNullOrEmpty(userInfo) && userInfo.Contains(':'))
{
request.Credentials = new NetworkCredential(userInfo.Split(':').First(), userInfo.Split(':').Last());
}
呀,我有一個類似的代碼 - 我只是不明白爲什麼HttpWebRequest忽略這個... – friism
我認爲這是一個安全問題。許多瀏覽器現在也忽略了這一點:http://support.microsoft.com/kb/834489我不得不回頭看看以前的.NET版本,看它是否被編碼使用它,但它明確不是在4.0 –