views:

702

answers:

2

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?

+2  A: 

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.

John Gietzen
A: 

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.

Matthew Whited