0

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.

0

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

|improve this answer|||||
  • 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. – Swetha Paritala Feb 4 at 6:20
0

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.

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

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.