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?CommentedFeb 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
      CommentedFeb 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?CommentedMar 15, 2023 at 9:50
    • I definitely would and I would apreciate it too!
      – Vertamin
      CommentedMar 15, 2023 at 10:33

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.