0
votes

I need to create a windows application which will access SharePoint 2010 site page to stote that page in ".htm" format in local drive.

But, that SharePoint site has claim based authentication.

For accessing that site I have to provide "Username" & "Password" along with "Domain name".

So, I need help on how can I pass site credentials through windows application to get access into that site??

I have used following code but this throws exception "The remote server returned an error: (403) Forbidden".

WebRequest Request1;
HttpWebResponse Response1;

Request1 = WebRequest.Create(txtUrl.Text.ToString());
Request1.Credentials = new NetworkCredential(strUserNm.ToString(), strPassword.ToString(), StrDomain.ToString());

Request1.PreAuthenticate = true;
Response1 = (HttpWebResponse)Request1.GetResponse();

Any help will be appreciable...

2
Don't pass in the domain only pass the username , password.. domain can be captured by calling the following I will post an example belowMethodMan

2 Answers

1
votes

Based on Warmup timer job with dual authentication mode, I changed this:

WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.Timeout = System.Threading.Timeout.Infinite;
request.Credentials = CredentialCache.DefaultNetworkCredentials;
WebResponse response = request.GetResponse();

to this:

HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.Timeout = System.Threading.Timeout.Infinite;
request.Credentials = new CredentialCache
{
    { new Uri(url), "NTLM", new NetworkCredential(username, password, domain) }
};
request.Headers.Add("Authorization", GetAuthorization(username, password, domain));
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 GTB7.1 ( .NET CLR 3.5.30729)";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.CookieContainer = new CookieContainer();
HttpWebResponse response = request.GetResponse() as HttpWebResponse;

The GetAuthorization method looks like this:

private function GetAuthorization(string username, string password, string domain) 
{
    string credentials = string.Format(@"{0}\{1}:{2}", domain, username, password);
    byte[] bytes = Encoding.ASCII.GetBytes(credentials);
    string base64 = Convert.ToBase64String(bytes);
    return = string.Concat("NTLM ", base64);
}

With the updated code, I was able to access files in both Classic and Claims Based web applications.

0
votes

Pay attention to how the domain is being captured..

//Do not use domain\username or username@domain for the username parameter of this constructor. This will not work (by design). The constructor that takes a domain has 3 arguments. Or use the domain property as clearly documented in the sample code above.

// Call the onstructor  to create an instance of NetworkCredential with the 
// specified user name and password.

NetworkCredential myCredentials = new NetworkCredential(username,passwd);

// Create a WebRequest with the specified URL. 
WebRequest myWebRequest = WebRequest.Create(url);
myCredentials.Domain = domain;
myWebRequest.Credentials = myCredentials;
Console.WriteLine("\n\nCredentials Domain : {0} , UserName : {1} , Password : {2}",
myCredentials.Domain, myCredentials.UserName, myCredentials.Password);
Console.WriteLine("\n\nRequest to Url is sent.Waiting for response...");


// Send the request and wait for a response.
WebResponse myWebResponse = myWebRequest.GetResponse(); 

// Process the response.
Console.WriteLine("\nResponse received successfully.");
// Release the resources of the response object.
myWebResponse.Close();