4
votes

I am working in a .NET web application.I am trying to connect to Sharepoint Online Premise to get documents in the "Document Library" by using the HTTPClient and the Sharepoint 2013 REST API .

I tested the following rest service in the browser for this ,

   https://site_url/_vti_bin/ListData.svc/Documents

which gives the MetaData of all the documents present in the Document Library of the particular web site.

Now when i am trying to do the same programmatically by adding a reference to the service URL and by using the following code,

TestSiteDataContext ctx = new TestSiteDataContext(new Uri("https://site url/_vti_bin/ListData.svc"));
ctx.Credentials = new NetworkCredential("[email protected]", "test");
var data = ctx.Documents.ToList();

I am getting the following exception : "The request was aborted: Could not create SSL/TLS secure channel." The stack trace is as follows :

at System.Net.HttpWebRequest.GetResponse()
   at System.Data.Services.Client.QueryResult.Execute()
   at System.Data.Services.Client.DataServiceRequest.Execute[TElement](DataServiceContext context, QueryComponents queryComponents)
   at System.Data.Services.Client.DataServiceQuery`1.Execute()
   at System.Data.Services.Client.DataServiceQuery`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at SPO_AccessAWebService.Program.Main(String[] args) in Location\Program.cs:line 35
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

I have gone through various articles/blogs to find a solution for this.Bust couldn't find any. I could see various samples tha shows authenticating by means of ,

ctx.Credentials = CredentialCache.DefaultNetworkCredentials;

This also is throwing the same error.

Is there is any other way to authenticate the SharePoint 2013 REST API?

Can someone provide me some insights to resolve this issue?

1

1 Answers

0
votes

Based on the exception message you are reporting, it appears that the SSL cert is not valid for the site. I often see this message in development or test environments where self-signed cents are used. You should be able to tell if the cert is not valid if you have to click through a warning message when you visit the URL in a browser. To fix the issue, you can try connecting over http if transport security isn't a concern, obtain or install a valid cert, or import the current cert into Trusted Root Authorities store of the Current User or Local Machine.