CSOM C# with Multi Factor Authentication

ASR 651 Reputation points
2021-05-24T09:23:38.76+00:00

Hi,
I have an application (webservice c#) to communicate with SP Online Tenant to fetch all site collections. All was working fine until client enable multi factor authentication.

How we can fetch all site collection from tenant having multi factor authentication enabled.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,158 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
18,643 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MichaelHan-MSFT 17,996 Reputation points
    2021-05-25T01:48:17.243+00:00

    Hi @ASR ,

    Using class SharePointOnlineCredentials with userName and password to authenticate in SharePoint does not support for MFA enviroment.

    As a workaround, you could install the package SharePointPnPCoreOnline: https://www.nuget.org/packages/SharePointPnPCoreOnline. And use the below way to get clientcontex.

       string siteUrl = "https://<tenant-name>.sharepoint.com/sites/test";    
       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);    
    

    Another workaround is using App Only autentication: https://www.c-sharpcorner.com/article/connect-to-sharepoint-online-site-with-app-only-authentication/


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. MichaelHan-MSFT 17,996 Reputation points
    2021-05-25T06:43:01.327+00:00

    Hi @ASR ,

    Then you could create app password for the account, use the app password to authenticate. Read this article for more: https://learn.microsoft.com/en-us/azure/active-directory/user-help/multi-factor-authentication-end-user-app-passwords