0

To download file from SharePoint Online, I tried to access it by .NET Core 6 Web API CSOM and here is my sample code.

using Microsoft.SharePoint.Client

private void DownloadFileFromSPO()
    {
        string site = "https://domain.sharepoint.com/sites/xxx";
        string user = "userID";
        SecureString securePwd = new SecureString();
        string password = "passward";
        foreach(char c in password.ToCharArray())
        {
            securePwd.AppendChar(c);
        }

        using (ClientContext context = new ClientContext(site))
        {
            context.Credentials = new NetworkCredential(user, securePwd);
            Microsoft.SharePoint.Client.File file context.Web.GetFileByUrl(fileUrl);
            context.Load(file);
            context.ExecuteQuery();
            //do something
            }
        }
    }

Unfortunately, when I called this method but got a http error 403: FORBIDDEN. Refer to this article, error 403 means server telling you, “I’m sorry. I know who you are–I believe who you say you are–but you just don’t have permission to access this resource.

So I supposed my API was trying to access correct website with correct userID and passward, but do not have authentication to access it. I also found there were many articles suggest replacing NetworkCredential(user, securePwd); by SharePointOnlineCredentials (user, securePwd);. However, when I tried this solution, I found my installed CSOM package did not have this class.

I also found MS docs said SharePointOnlineCredentials is not available anymore, they recommend another "OAuth access token" way to get the authentication.

My question is, is that the only way to get the authentication to access SharePoint Online? Due to company policy, configuring an application in Azure AD process is bothering me. Is there another solution, just by code and package, in this scenario?

Evan Hsiao
  • 29
  • 10
  • Actually I can access SPO now by bearing OAuth access token from Azure AD apps, so I still follow MS commanded process to get into SPO. – Evan Hsiao Feb 18 '22 at 06:43

0 Answers0