The remote server returned an error: (403) Forbidden error

client-sidesharepointsharepoint-2013

I am developing a C# console application on my local computer running on Windows 8. My target SharePoint server is SharePoint Online on Office 365. I am trying to get the web site name using web.Title property of CSOM of SharePoint 2013. I wrote the following code inside the main function to do that.

ClientContext clientContext = new    ClientContext("https://innergen.sharepoint.com/sites/mydevsite");
    Web web = clientContext.Web;
    clientContext.Load(web);
    clientContext.ExecuteQuery();

    Console.WriteLine("Web Site Name : " + web.Title);
    Console.ReadLine();

Now when I run my project in debug mode, I receive an error saying "The remote server returned an error: (403) Forbidden". I used the same Microsoft Account to log in to both my SharePoint Online account and Windows 8 OS(local machine). Can anybody tell me what's wrong here? Thanks.

Best Answer

Try to add credentials to your clientContext object

NetworkCredential credentials =
     new NetworkCredential("username", "pwd", "domain");
clientContext .Credentials = credentials;

or use

clientContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
Related Topic