C# Programmatically Authenticate SharePoint Online account enabled with MFA.

Suresh S 1 Reputation point
2021-07-21T04:58:18.24+00:00

Hi All,

We have developed web application in ASP.net C# and all the documents are storing in SharePoint Online. As per the requirement, the documents details should be
taken from SharePoint Online and display in the application. We have an account in SharePoint Online enabled with MFA feature. The code has been authentication. But I am getting error.

Code:

ClientContext ctx = new ClientContext(strSiteURL);
SecureString secureString = new SecureString();
strPassword.ToList().ForEach(secureString.AppendChar);
ctx.Credentials = new SharePointOnlineCredentials(strUserID, secureString);
Site site = ctx.Site;
ctx.Load(site);
ctx.ExecuteQuery();

Error:
The sign-in name or password does not match one in the Microsoft account system.

I want to authenticate the SharePoint Online via MFA enabled user credentials without the OTP prompt or call.

How can I make it possible?

Thanks & Regards
Suresh Sankaran

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,737 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,686 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,576 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MichaelHan-MSFT 18,016 Reputation points
    2021-07-21T07:02:09.057+00:00

    Hi @Suresh S ,

    With MFA enabled, the method using class SharePointOnlineCredentials with username and password for authentication would not work.

    You have only two options:

    1. Use user credentials with the OTP prompt or call. This needs to install the package SharePointPnPCoreOnline: https://www.nuget.org/packages/SharePointPnPCoreOnline. 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);

    2.Use app-only for authentication: https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs


    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. Suresh S 1 Reputation point
    2022-06-22T05:08:45.353+00:00

    Hi,

    I have explored App only and App password. But App Password will be deprecated from October 22 onwards. Is there any other approach except App Only.

    0 comments No comments