Whether we're talking about C# or PowerShell, we need to authenticate in order to interact with SharePoint Online.
Right now, Azure Control Service (ACS) is still available but it is deprecated and scheduled for retirement. It should function until April 2nd, 2026, or until it is explicitely disabled via PowerShell Command. More on ACS here.
To connect to SharePoint Online via ACS I suggest this article,
https://www.c-sharpcorner.com/article/connect-to-sharepoint-online-site-with-app-only-authentication/
Since ACS is old news, you may want to switch to the new Azure AD App.
The process is not easy but it is well documented in this article where Microsoft describes the step-by-step process of switching from ACS to Azure AD App,
https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins-modernize/from-acs-to-aad-apps
There's a lot of misconceptions around CSOM but for now it's still alive and kicking with no end in sight as you can see here.
That said, if what you're trying to accomplish doesn't need to be done via C#, you can switch to PowerShell and connect via PnP PowerShell, which sort of works as its own app, where you connect interactively and run your scripts, just like in C# but in a PowerShell script, no app needed. You'll obviously need access rights to the site and the ability to run custom scripts. PnP supports many authentication methods, including ACS and Azure AD Apps, but you can connect without them if you have access and are ok with the interactive prompt.
Example:
Install-Package -Name PnP.PowerShell
connect-pnponline -url https://contoso.com -interactive
$events = Get-PnPListItem -list Events
More details here,
https://www.sharepointdiary.com/2019/07/how-to-run-powershell-scripts-for-sharepoint-online.html
PowerShell PnP Module,
https://github.com/pnp/powershell
Other notes
- Different PowerShell modules connect using a different context
(Microsoft.Online.SharePoint.PowerShell connects you as an
admin, PnP.PowerShell uses whatever credentials your provide,
which can be your own)
- Make sure the account or credential that you use to connect actually has access rights to the resources, otherwise you'll keep getting issues where the connection works but the data is not returned
- Interestingly enough, you can use the PowerShell "app" to connect to
a SharePoint site interactively via C# using the code below
- The "right" way to authenticate to SharePoint Online going forward is probably the creation of the Azure AD App, so you should give it a try if it's an option for you. You may need an admin to help with consenting the rights if you're not one yourself
.
var authManager = PnP.Framework.AuthenticationManager.CreateWithInteractiveLogin("31359c7f-bd7e-475c-86db-fdb8c937548e");
ctx = authManager.GetContext(url);