Getting 403 forbidden error when trying to connect to customer sharepoint site using C# CSOM despite having access via web/Teams

Matt Thesing 1 Reputation point
2022-08-30T04:24:17.103+00:00

I am writing a C# console application that lists and then downloads files from a customer's SharePoint site. When I try to connect to their SharePoint site, I get a 403 Forbidden error. The same code works find when I connect to one of my company's SharePoint site.

Both sites use the same AD/Office 365 credentials (myusername@mycompany.com). I can navigate to the customer's SharePoint site in a browser and am able to view and download the files there using those credentials. But when I try to do the same thing via code, I get the 403 error.

Here is the code I am using:

using (ClientContext context = new ClientContext("https://customer.sharepoint.com/sites/SiteToReadFilesFrom/"))  
{  
    context.AuthenticationMode = ClientAuthenticationMode.Default;  

    string account = "myusername@mycompany.com";  
    string password = "mypassword";  

    var secret = new SecureString();  
    foreach (char c in password)  
    {  
        secret.AppendChar(c);  
    }  
    context.Credentials = new SharePointOnlineCredentials(account, secret);  
    context.Load(context.Web);  
          
    context.ExecuteQuery();      <!-- throws the error  
}  

If I switch the URL in the ClientContext to one of my SharePoint sites, it works fine. And I have access to the site and can view/download via a web browser (or Teams - this is a SharePoint site attached to a Team that I was added to).

I feel like I'm missing something obvious here. Is there a way to specify which AD Directory I am authenticating against? I have no control over the customer's site (or AD for that matter) so I have to solve this on my side unless is is a very simple request to change something on their side (like permissions).

Any and all help would be greatly appreciated.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,279 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,675 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,116 Reputation points
    2022-08-30T08:14:13.703+00:00

    Hi @Matt Thesing ,

    According to my research and testing, I can use the code you provided well and successfully connect to SharePoint. Please try to do a troubleshooting:

    1. Make sure you have permission to this site.
    2. Make sure you are using the latest Microsoft.SharePoint.Client dlls

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.