CSOM C# with Multi Factor Authentication

Amit Singh Rawat 731 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.

Microsoft 365 and Office | SharePoint | For business | Windows
Microsoft Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments
{count} votes

2 answers

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

    Hi @Amit Singh Rawat ,

    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 18,126 Reputation points
    2021-05-25T06:43:01.327+00:00

    Hi @Amit Singh Rawat ,

    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


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.