I have the following code which works perfectly fine with Forms Based Authentication turned off, but the moment I turn it on it stops working:
string baseURL = siteUrl + "/_api/";
string uriString = "web/lists/GetByTitle('" + listName + "')/fields/getbyinternalnameortitle('" + displayName + "')";
NetworkCredential cred = CredentialCache.DefaultNetworkCredentials;
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(new HttpClientHandler()
{
Credentials = cred
});
client.BaseAddress = new Uri(baseURL);
client.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
};
HttpResponseMessage resp = client.GetAsync(uriString).Result;
string respString = resp.Content.ReadAsStringAsync().Result;
The response I get is as follows:
{StatusCode: 403, ReasonPhrase: 'Forbidden', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Transfer-Encoding: chunked
X-SharePointHealthScore: 0
X-Forms_Based_Auth_Required:...)
I have tried several things, including specifying the farm account Windows Authentication credentials under on the HttpClient Credentials object, I have also tried to use the AuthenticationHeaderValue:
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _FormatBasicAuth("domain", "username", "password"));
Any help will be highly appreciated.