Udostępnij za pośrednictwem


How to handle - The remote server returned an error: (403) Forbidden while connecting to SharePoint Online (O365)

Recently while working on SharePoint, I found when I try to connect to SharePoint Online from console application using CSOM and try authenticating using default network credential, I got 403 forbidden.

After looking bit into it, I realized it is because of the approach I was using to authenticate against the SharePoint Online.

In case of SharePoint online, If you need to create ClientContext using CSOM then you can use one of the following approaches

1) Using SharePointOnlineCredentials

Sample code snippet -

 SecureString password = new SecureString();
 
 
 foreach (char c in "yourpassword".ToCharArray())
 password .AppendChar(c);
 
 clientContext.Credentials = new SharePointOnlineCredentials("yourUserName@yourDomain.onmicrosoft.com", password);
 

2) Using AccessToken

In this case, Accesstoken gets used for creating the client context. In case you do not have access AccessToken and need access permissions on the fly, I would recommend you to read https://msdn.microsoft.com/en-us/library/jj687470.aspx