[英]Download using WebClient - IIS Basic Authentication
我用這個從IIS上發布的網站下載html內容:
using (var client = new WebClient())
{
client.Credentials = CredentialCache.DefaultCredentials;
string html = client.DownloadString("http://site.com");
}
但是當IIS設置為基本身份驗證時,這不起作用。 用戶已在IIS對話框中鍵入用戶和密碼。
有一種方法可以使這項工作不再通過用戶和密碼?
我懷疑基本身份驗證需要密碼,所以我懷疑你需要一個new System.Net.NetworkCredential("your-username","your-password")
。 默認憑據適用於集成的身份驗證,但不適用於(AFAIK)基本身份驗證。 所以回答:
有一種方法可以使這項工作不再通過用戶和密碼?
不,我不這么認為。
搜索更多,我發現了這個:
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;
有一天,我研究了這個問題並發現它有效。 但是,使用.Net 4.0,我的Visual Studio說GetSystemWebProxy
目前已被棄用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.