2

When connecting to online site we have Microsoft authentication call to our phones and we will enter our pin for authenticating.Then only we can access the site.

But in csom code when conencting to site I am getting the error:

"Exception calling "ExecuteQuery" with "0" argument(s): "The sign-in name or password does not match one in the Microsoft account system.".

Here is my code:

$ctx=New-Object Microsoft.Sharepoint.Client.ClientContext($siteCollUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $securePassword)
$web=$ctx.Web
$ctx.Load($web)
$ctx.ExecuteQuery()

How can I access the site if MFA is enabled through csom pwoershell. please suggest.

5

If you are using PnP powershell you can use the below code,

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

From CSOM:

static void Main(string[] args) 
{ 
   string siteUrl = "https://< Jump 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); 
}

Source

1
  • How can use OfficeDevPnP.Core library in Csom Powershell. I tried searching for this library to use in csom powershell code. I did not find any.Please Suggest. Feb 4 '20 at 6:20
3

You can use SharePoint Online Management Shell to connect with multi-factor authentication (MFA) SharePoint Site.

Follow below documentation:

To connect with multi-factor authentication (MFA).

OR

Using UseWebLogin:

Connect-PnPOnline -Url $siteUrl –UseWebLogin

enter image description here

OR

Using App Password:

Also, you can use App password in place of normal password to login easily.

I am using App Password for authentication in PowerShell as well as SharePoint Designer and it works well.

Create an app password for Office 365.

6
  • Hi Swetha, Have you tried this? Is it working for you?? Jan 31 '20 at 12:27
  • tried using property -web login.and it working well.Thanks for the solution.:-) Feb 4 '20 at 5:54
  • Great, glad it worked for you. But I didn't get why you accepted and again unaccepted my answer? Feb 4 '20 at 6:54
  • Solutions provided from both are correct.I marked both as correct.I think only one we can mark as answer.- Feb 5 '20 at 6:24
  • 1
    This should be the accepted answer Jul 27 at 17:19
0

I came across the same error today, when implementing an CSOM console app that would connect to my tenant.

Searching through the net, i came across multiple answers that pointed out tha MFA was the source of the problem.

Searching through the cause i found out that an Admin had blocked all of the legacy Login Methods from the Conditional Access Blade in order to increase the security score of our tenant.

That was basically the problem that caused the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.