Skip to main content
Notice removed Canonical answer required by CommunityBot
Bounty Ended with no winning answer by CommunityBot
edited tags
Link
Notice added Canonical answer required by Rafał Saltarski
Bounty Started worth 50 reputation by Rafał Saltarski
Tweeted twitter.com/#!/StackSharePoint/status/289537987564429312
Source Link
Rafał Saltarski
  • 1.4k
  • 2
  • 23
  • 42

SharePoint 2013 and anonymous access problem

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