In this article, we will learn how to authenticate to SharePoint online using C# coding through the .Net console application. Microsoft SharePoint Online is Software as a Service (SAAS) which is part of Microsoft 365 (formerly known as Office 365). The various way we can connect or authenticate to SharePoint Online – CSOM (Client Side Object Model) is one of the approaches, in this article, we will learn how we can connect to SharePoint online remotely using the CSOM (Client Side Object Model) C# coding.
Before getting into this article, you may also want to read this: Top 5 SharePoint Online Authentication Methods You Need to Know About
How to authenticate to SharePoint online using c# coding
using Microsoft.SharePoint.Client; using System; using System.Linq; using System.Security; using System.Linq; using System.Text; namespace ConsoleApp1 { class Program { static void Main(string[] args) { string siteCollectionUrl = "https://globalsharepoint2020.sharepoint.com"; string userName = "YourSPOUserName@yourtenant.onmicrosoft.com"; string password = "YourSPOPassword"; Program obj = new Program(); try { obj.ConnectToSharePointOnline(siteCollectionUrl, userName, password); } catch (Exception ex) { string msg = ex.Message.ToString(); } } public void ConnectToSharePointOnline(string siteCollUrl, string userName, string password) { //Namespace: It belongs to Microsoft.SharePoint.Client ClientContext ctx = new ClientContext(siteCollUrl); // Namespace: It belongs to System.Security SecureString secureString = new SecureString(); password.ToList().ForEach(secureString.AppendChar); // Namespace: It belongs to Microsoft.SharePoint.Client ctx.Credentials = new SharePointOnlineCredentials(userName, secureString); // Namespace: It belongs to Microsoft.SharePoint.Client Site mySite = ctx.Site; ctx.Load(mySite); ctx.ExecuteQuery(); Console.WriteLine(mySite.Url.ToString()); } } }
Note:
We must add the below two DLLs in the project assemblies by clicking on the project name from the right-side solution explorer panel (right-click on the “Dependencies” then click on the “Add Reference”):
- Microsoft.SharePoint.Client
- Microsoft.SharePoint.Client.Runtime

Sometimes, we might get the “The remote server returned an error: (400) Bad Request.” error while running the above code – so, how to fix this error?
How to fix the remote server returned an error: (400) Bad Request error (Authentication in SharePoint online)?
In order to fix the above, “the remote server returned an error: (400) Bad Request error”, we need to change the project target framework by going to the project tab, then clicking on the project property menu (here it is “ConsoleApp1 Properties”), then finally we need to click on the “Application” menu from the left side panel.
Then from the “Target framework” section, we have to change the framework, if you get the above error change the framework to 4.5 and build the solution again and debug the code again, now we should not face this error.

Another way of authenticating to SharePoint Online: App Only Authentication (Authentication in SharePoint online)
In-App Only Authentication we need to create the “client-id” and “client secret”, which can be configured in two steps:
- Setup app-only principal
- Grant permissions to the newly created principal
Setup app-only principal
Basically, this step is responsible to create the client id and client secret id.
-
Open the appregnew.aspx page (https://sposite.sharepoint.com/_layouts/15/appregnew.aspx) - here get the client id and client secret id.
- Assign permission to the app from the appinv.apsx page
(https:// sposite.sharepoint.com/_layouts/15/appinv.aspx)
Note:
- For the tenant level scoped permission – we need to open the URL like this –
https://yourtenant-admin.sharepoint.com/_layouts/15/appinv.aspx
Grant permissions to the newly created principal
In this step, we need to assign permission to the app, the permission could be any of the below:
- Read
- Write
- Manage
- FullControl
Along with the permission, we need to specify the scope of the permission, the scope could from any of the below:
http://sharepoint/content/sitecollection - site collection level http://sharepoint/content/sitecollection/web - web level http://sharepoint/content/sitecollection/web/list - list level http://sharepoint/content/tenant - tenant level
Example:
<AppPermissionRequests> <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/" Right="Write"/> </AppPermissionRequests>
Note:
- For details on app-only authentication, please refer to this article: In 4 steps access SharePoint online data using postman tool
Summary: How to authenticate to SharePoint online using c# coding
Thus in this article, we have learned the below with respect to connecting to SharePoint Online using the CSOM (client-side object model) coding:
- How to authenticate to SharePoint online using C# CSOM coding
- How to connect to SharePoint Online using the C# CSOM (client-side object model) coding
- How to fix the remote server returned an error: (400) Bad Request error?
- How to access or authenticate SharePoint Online site data using the app-only authentication model.
See Also: How to authenticate to SharePoint online using c# coding
Looking for a PowerApps tutorial from the scratch? Then you are in the right place, below are a bunch PowerApps articles you may like:
- How to send email from PowerApps button click?
- How to open outlook from PowerApps step by step?
- Create your first chatbot in PowerApps step by step
- Cascading dropdown in PowerApps using SharePoint data
- CRUD operations in PowerApps using SharePoint online list
- CRUD Operation in PowerApps Using Excel OneDrive
- Phone number and email validation in PowerApps
- Connect to a SharePoint list in PowerApps step by step
- Collection variable in PowerApps
- Understand the difference between Set and UpdateContext function
- String concatenation function in PowerApps
- 3 ways to create Power Apps – Types of Power Apps
- Overview view of PowerApps development environment in Power Platform
- Customize SharePoint List Forms Using PowerApps step by step – Office 365
- Create free development environment using Power Apps Community Plan
- Show or hide columns conditionally in SharePoint list form
- SharePoint Online – Power Automate: How to Export and Import Microsoft Team Flows across environment
- Authentication in sharepoint online