Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending HTTP Headers with HTTP Web Request for NTLM Authentication

I want to login to a Sharepoint portal which brings up a login dialog but is using NTLM authentication. How can I modify the HTTP headers in C# to make a successful login request? I assume I would need to make a HTTPWebRequest to a page within the logged in section of the portal and post the HTTP headers collection alongside this?

like image 918
blade Avatar asked Oct 05 '09 15:10

blade


2 Answers

You can do this using the WebRequest class.

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();

This code reads the whole response into a variable called token.

like image 147
John Gietzen Avatar answered Nov 15 '22 12:11

John Gietzen


To use NTLM see John's answer. If you need to have headers across sessions look at the CookieContainer property on the HttpWebRequest object. You will need to keep a reference to your CookieContainer and attach it to any other HttpWebRequests you make.

like image 20
Matthew Whited Avatar answered Nov 15 '22 14:11

Matthew Whited