Skip to content
Menu
SharePoint Gems
  • Power Platform
  • Development
    • SharePoint 2013
  • Migration
  • Administration
  • SP Online
  • Contact
  • About
    • Privacy Policy
SharePoint Gems

Connect MFA enabled SharePoint site using CSOM

Posted on November 6, 2020September 29, 2021

Hello SharePointers,
Today we are going to discuss How to connect MFA enabled SharePoint site using CSOM in SharePoint Online.

Contents hide
1 Introduction
2 Connect so site with MFA using CSOM
2.1 Share this:
2.2 Like this:
2.3 Related

Introduction

Usually, we write new scripts and utilities to perform certain tasks or to automate the business process in SharePoint Online.

We often implement some tools which always run with particular credentials (usually SharePoint administrator or Site Collection administrator) which have the higher privileges. In this process we need to store the credentials at a Central credentials Manager.

For a simple process, we often pass credentials in a code (hard code) else them in configuration files such as app.config or web.config.

By this way of authentication doesn’t work if Multi-Factor Authentication is enabled for that specific site. How to connect to a SharePoint site with MFA enabled?

Here the SharePoint PnP comes and make our life easier. Lets see some code snippets below to connect MFA enabled SharePoint site using CSOM.

Connect so site with MFA using CSOM

Below is the sample code using CSOM within a console app. The same code can be used to build windows forms or some other applications using CSOM.

Within a namespace called OfficeDevPnP.Core, there is an Authentication Manager class which has many helper methods to create SharePointContext object with different authentication methods.

In the below code, we are going to use the method GetWebLoginClientContext(SiteUrl). This will work for almost all types of authentication scenarios.

This method will pop up a dialog to enter a UserName and Password for the site. It will ask for an authentication token.

Below are the steps need to follow.

STEP 1: Install “SharePointPnPCoreOnline” NuGet package to the visual studio solution.

Connect MFA enabled SharePoint site using CSOM

STEP 2: Copy and Paste below code into Program.cs file. (change URL attribute with your actual URL).

static void Main(string[] args) 
{ 
   string siteUrl = "https://<tenant-name>.sharepoint.com/sites/MFADemo"; 
   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); 
}

after successfully done the code, Build a Solution and Run it.

STEP 3: The running solution will prompt you to a dialog box for the credentials. Enter your credentials and provide an authentication token.

Connect MFA enabled SharePoint site using CSOM

Yes, and all set. run your code and you will see output as below.

Connect MFA enabled SharePoint site using CSOM

Our other posts are below.

  • Delete all items from SharePoint list with CSOM
  • Retrieve all users from Site Collection using PowerShell
  • How To Save Records In Multiple List Using Power Apps Patch() function
  • Get Current Context from SharePoint Modern Site Page
  • Solved: Send approval request to a group in MS flow.

Share this:

  • Print
  • Twitter
  • Facebook
  • LinkedIn
  • WhatsApp
  • Telegram
  • Pinterest
  • Reddit

Like this:

Like Loading...

Related

4 thoughts on “Connect MFA enabled SharePoint site using CSOM”

  1. Pingback: Retrieve SharePoint List Items using CSOM - SharePoint Gems
  2. Pingback: SharePoint Online: Edit Permission Level with CSOM PowerShell - SPGem
  3. Pingback: SharePoint Online: Identify Current Page is Modern page or Classic page - SharePoint Gems
  4. Pingback: Check date field is empty in Nintex Workflow - SharePoint Gems

Leave a ReplyCancel reply

  • Development
  • Migration
  • Poweer Apps
  • Power Automate
  • Power Platform
  • SharePoint 2013
  • SharePoint Administration
  • SharePoint Online
  • Tips
  • Uncategorized

Best of Computers

Blog Stats

  • 67,509 hits

Subscribe

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,433 other subscribers
Buy Me Coffee
©2025 SharePoint Gems | Powered by WordPress and Superb Themes!
%d