c# – 使用HTTP Web請求傳送HTTP標頭以進行NTLM身份驗證

我想登入到一個Sharepoint門戶,它會顯示一個登入對話方塊,但是正在使用NTLM身份驗證.如何修改C#中的HTTP標頭以成功登入請求?我假設我需要對門戶登入部分中的頁面進行HTTPWebRequest,並將HTTP頭集合與此一起釋出?

您可以使用WebRequest類執行此操作.

WebRequest req = WebRequest.Create(tokenUri);
req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
req.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
WebResponse resp = req.GetResponse();
StreamReader reader = new StreamReader(resp.GetResponseStream());
var token = reader.ReadToEnd().Trim();

此程式碼將整個響應讀入名為token的變數中.

翻譯自:https://stackoverflow.com/questions/1520703/sending-http-headers-with-http-web-request-for-ntlm-authentication