I am trying to connect SharePoint 2013 Online website using a Context Token from a console executable. However, it is giving me error The remote server returned an error: (403) Forbidden.

Here is the code snippet:

string spurl = ConfigurationManager.AppSettings["Sharepoint_URL"].ToString();
using (ClientContext context = new ClientContext(spurl))
{
    context.Credentials = new NetworkCredential("username", "password", "domain");
    context.ExecuteQuery();
    Web web = context.Web;
    context.Load(web);
    context.ExecuteQuery();
    Console.WriteLine(context.Web.Title);
 }               
 Console.ReadKey();

How can I make a connection with SharePoint Online? Does it only support claims-based authentication?

有帮助吗?

解决方案

I think the method with which you are trying to authenticate is outdated. The SharePoint 2013 Client Object Model now has a class called SharePointOnlineCredentials which abstracts away all the tedious cookie container stuff. E.g.:

using (ClientContext clientContext = new ClientContext("https://yoursite.sharepoint.com/"))  
{  
    SecureString passWord = new SecureString();
    clientContext.Credentials = new SharePointOnlineCredentials("loginname@yoursite.onmicrosoft.com", passWord);
    Web web = clientContext.Web;
    clientContext.Load(web);
    clientContext.ExecuteQuery();
    Console.WriteLine(web.Title);
    Console.ReadLine();
} 

Please refer to this link for more information: https://sharepoint.stackexchange.com/questions/83985/access-the-sharepoint-online-webservice-from-a-console-application

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top