We run into following problem:
- We have application, that makes authenticated calls to sharepoint 2013 REST services (PUT and GET). Everything works well, when anonymous access is turned off.
- But we need to enable anonymous access for two or three lists. When this is done, request for FormDigest from C# code fails always with 403 result – forbidden.
I attach following code that is used for making the call. Again, it works fine when Anonymous Access is disabled for this site collection.
var contextContent = new StringContent(string.Empty,Encoding.UTF8,"text/xml"); var contextInfoRequest = await contextInfoClient.PostAsync(url + "/_api/contextinfo", contextContent); var contextInfoRequestStr = await contextInfoRequest.Content.ReadAsStringAsync(); var formDigestXml = XDocument.Parse(contextInfoRequestStr); var formDigestNode = formDigestXml.Descendants(d + "FormDigestValue").First(); var formDigest = formDigestNode.Value; var itemPostBody = Encoding.UTF8.GetBytes("{'__metadata':{'type':'" + entityTypeName + "'}, " + jsonProperties + "}"); var addRequest = (HttpWebRequest)WebRequest.Create(queryUrl); addRequest.Method = "POST"; addRequest.ContentType = "application/json;odata=verbose"; addRequest.Accept = " application/atom+xml"; addRequest.Headers["X-RequestDigest"] = formDigest; addRequest.Credentials = new NetworkCredential(user, pass, domain); var itemRequestStream = await addRequest.GetRequestStreamAsync(); itemRequestStream.Write(itemPostBody, 0, itemPostBody.Length);
// Following line causes 403 Frobidden Exception:
var itemResponse = (HttpWebResponse) await addRequest.GetResponseAsync();
var respStream = itemResponse.GetResponseStream();
var l = new List<byte>();
while (respStream.CanRead)
{
var i = respStream.ReadByte();
if (i == -1) break;
l.Add((byte)i);
}
var xml = Encoding.UTF8.GetString(l.ToArray(),0,l.Count);