1

I have a complex CSOM script that I need to get working for a Tenant that uses Modern Authentication.

This works fine using Basic Authentication. Its not an automated script so an interactive login is fine.

$Cred = Get-Credential
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName, $Cred.Password)  
... do rest of script...

Any idea how I change this to use modern authentication? Even temporaily switching off MFA still wont work.

I can log in fine using the credentials to the SP site.

Many thanks!

1 Answer 1

2

If you are using the old SharePoint dlls. Make sure you install or are using the last PnP powershell library.

Then you can use the following code to prompt the MFA connection:

Connect-PnPOnline -Url $siteUrl –UseWebLogin
$context = Get-PnPContext

$web = $context.web
$context.load($web)
$context.ExecuteQuery()
4
  • Hi Vertamin, Thanks for your comment. However, this means I then have to use PnP code rather than CSOM doesnt it? I.e. you cant login using Pnp and then use CSOM can you? Feb 28, 2023 at 12:05
  • PNP tools once installed, give you access to all the other CSOM code too. Its included in the library. So you could use both pnp commands and csom code, they are compatible.
    – Vertamin
    Feb 28, 2023 at 14:35
  • Thanks Vertamin. I gave up and used PnP code but its good to know that PnP gives access to the CSOM code too. Might be good to put this down as an answer? Mar 15, 2023 at 9:50
  • I definitely would and I would apreciate it too!
    – Vertamin
    Mar 15, 2023 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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