CSOM does not use windows credential and giving "The remote server returned an error: (403) Forbidden." error

Viewed 1270

I have a console application with .NET Framework which uploads a file using CSOM. When I run the application I get below error:

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

However, When I pass my windows credential it works fine. I don't want to hardcode my credential in code due to security purpose. Is there any work around except passing credential in the code?

My code is as below:

using System;
using System.IO;
using System.Linq;
using System.Security;
using Microsoft.SharePoint.Client;
using ClientOM = Microsoft.SharePoint.Client;


namespace ConsoleApp3
{

class Program
{
    static void Main(string[] args)
    {

        ClientContext clientContext = new ClientContext("https://companyname.sharepoint.com");

        //SecureString passWord = new SecureString();
        //foreach (char c in "HelloWorld@1234".ToCharArray()) passWord.AppendChar(c);
        //clientContext.Credentials = new SharePointOnlineCredentials("jay.desai@company.com", passWord);
        clientContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

        using(FileStream fileStream = new FileStream(@"C:\Users\jay.desai\Desktop\LSRSQL01_ACXM_20201003.html", FileMode.Open))
            ClientOM.File.SaveBinaryDirect(clientContext, "/sites/DataServices/Shared Documents/Data Dictionaries/LSRSQL01_ACXM_20201003.html", fileStream, true);
    }
}

}

1 Answers

CSOM connect SharePoint Online requires SharePointOnlineCredentials. Test demo:

static void Main(string[] args)   
      {  
            string userName = "amos@contoso.onmicrosoft.com";  
            Console.WriteLine("Enter your password.");  
            SecureString password = GetPassword();                  
            using(var clientContext = new ClientContext("https://contoso.sharepoint.com"))   
            {  
                // SharePoint Online Credentials  
                clientContext.Credentials = new SharePointOnlineCredentials(userName, password);  
                // Get the SharePoint web  
                Web web = clientContext.Web;  
                // Load the Web properties  
                clientContext.Load(web);  
                // Execute the query to the server.  
                clientContext.ExecuteQuery();  
                // Web properties - Display the Title and URL for the web  
                Console.WriteLine("Title: " + web.Title + "; URL: " + web.Url);  
                Console.ReadLine();  
                }  
            } 

private static SecureString GetPassword()  
      {  
            ConsoleKeyInfo info;  
            //Get the user's password as a SecureString  
            SecureString securePassword = new SecureString();  
            do   
            {  
                info = Console.ReadKey(true);  
                if (info.Key != ConsoleKey.Enter)   
                {  
                    securePassword.AppendChar(info.KeyChar);  
                }  
            }  
            while (info.Key != ConsoleKey.Enter);  
            return securePassword;  
        } 

Source:https://www.c-sharpcorner.com/article/connect-to-sharepoint-2013-online-using-csom-with-console-ap/

Updated:

string siteUrl = "https://contoso.sharepoint.com/sites/demo";
using (var cc = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, "[Your Client ID]", "[Your Client Secret]"))
{
    cc.Load(cc.Web, p => p.Title);
    cc.ExecuteQuery();
    Console.WriteLine(cc.Web.Title);
};
Related