We often write utilities and scripts for performing certain automation and tasks for a SharePoint site.
Authenticating a user with standard authentication mechanism is easy. For some tools, they always run with specific credentials (like an account with higher privileges), where the credentials are stored at a central credential manager. And for simpler ones, the usernames and passwords are used directly within a SharePointOnlineCredentials or NetworkCredentials object in CSOM code.
This way of authentication doesn’t work if the multi-factor authentication is enabled. So, how do we connect to a SharePoint site with multi-factor authentication enabled? It's SharePoint PnP which makes it happen. Below are some sample snippets.
Connecting to a site with MFA using CSOM
Below is the sample code using CSOM within a console app. The same can be extended for building Windows Forms or other applications using CSOM.
In the OfficeDevPnP.Core namespace, there is an AuthenticationManager class which has many helper methods for creating a SharePointContext object with different authentication types. The method GetWebLoginClientContext(String, Icon) is my favorite method, which works for almost any type of authentication scenario.
When we use this method, it opens a pop-up with the standard tenant login page, and users will be prompted for the credentials and challenges for the second-factor authentication. Once the user authenticates themselves, the authentication token is read by the AuthenticationManager and it prepares a ClientContext Object which we can use.
Install the ‘SharePointPnPCoreOnline’ NuGet package to the Visual Studio solution which installs the OfficeDevPnP.Core assembly.

Below is the code snippet to get a client context.
- static void Main(string[] args)
- {
- string siteUrl = "https://<tenant-name>.sharepoint.com/sites/contosoteam";
- var authManager = new OfficeDevPnP.Core.AuthenticationManager();
- // This method calls a pop up window with the login page and it also prompts
- // for the multi factor authentication code.
- ClientContext ctx = authManager.GetWebLoginClientContext(siteUrl);
- // The obtained ClientContext object can be used to connect to the SharePoint site.
- Web web = ctx.Web;
- ctx.Load(web, w => w.Title);
- ctx.ExecuteQuery();
- Console.WriteLine("You have connected to {0} site, with Multi Factor Authentication enabled!!", web.Title);
- }
When you run this code, it shows the following pop up with the tenant’s login page


Connecting to a site with MFA using PnP PowerShell
When using the Connect-PnPOnline cmdlet without any additional authentication parameters, we are prompted for username and password, which will not work if multi-factor authentication is enabled. We can use the following switch to show a web login for authentication which handles MFA.
Connect-PnPOnline -Url $siteUrl –UseWebLogin

Using the helper methods from the OfficeDevPnP.Core and PnP Powershell we can use a standardized solution to show a web login, which not only handles multi-factor authentication but also most of the authentication mechanisms.
For tools or scripts which need the user to login for every run, we can use this approach to handle MFA authentication. However, this may not be the right solution for scheduled jobs which shouldn’t/wouldn’t wait for a user to login and continue.

Madhu AbePosted Oct 31, 2024, 1:55 PM
I'm trying to connect SP Online site using OfficeDevPnP.Core inside SharePoint 2019 server (Installed on Windows server 2022). But the authentication popup is not prompting and i'm getting "The remote server returned an error: (403) Forbidden" Error. Does anyone experienced this ?
Ninad JoshiPosted Sep 28, 2022, 1:48 PM
Im getting 502 error while implementing this, can you please really help here Im stuck.
Ninad JoshiPosted Sep 12, 2022, 9:26 AM
Hi the logging pops up and disappears I am try to authenticate user by email id and password, how to do that with MFA enabled users.
Kapil JoshiPosted Jun 8, 2022, 12:58 PM
The pop doesnt stays on my screen ,The login window does pop up for a second, but closes again immediately. and then error comes "Cannot contact site at the specified URL "mylistaddress". There is no Web named "mylistaddress". Any solution sir..
Vinay AyinapurapuPosted Aug 19, 2020, 12:36 AM
Nice information.
Mike RodriguezPosted Jul 21, 2020, 9:09 PM
How do you handle authentication for scheduled jobs?
Karthik KPosted Jun 17, 2020, 7:51 AM
Hello Ram, This is working fine .. But When it is scheduled in task scheduler that is not working.
aniket barpetiaPosted Apr 22, 2020, 10:43 PM
Hello Ram, Thanks for article. I followed you steps but on authManager.GetWebLoginClientContext(siteUrl); I am getting "Error CS0012 The type 'Icon' is defined in an assembly that is not referenced.You must add a reference to assembly 'System.Drawing.Common, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'." Any idea to fix this? I am using console app project with VS 2019 Thanks.
Markus FieberPosted Dec 12, 2019, 4:50 AM
Thank you. How works this for MS Project? The ProjectContext Object is different then the ClientContext object from sharepoint
Manoj MittalPosted Aug 2, 2019, 8:57 PM
Thanks for details article. Did you try with any backend activity i.e. webjob which runs once in a day and adding token is not feasible. What's your thoughts ?
Ravivarma BalakrishnanPosted Aug 2, 2019, 12:46 PM
Informative One!
Bhargav ReddyPosted Aug 2, 2019, 9:04 AM
Very informative article. Keep them coming. :)