Sharepoint Online C# CSOM MFA Issues

Klein, John 20 Reputation points
2023-02-27T22:07:56.9533333+00:00

I keep going round and round. Microsoft keeps changing how things work, so even examples from 2022 no longer work.

All I need help with is doing Multi-Factor Authentication to Sharepoint Online through C#. All the examples I find have you using older packages that no longer work with the latest Visual Studio environment. I use NuGet to get my packages, and if I try to use the older methods, they are not supported.

The previous way I see it was done is the following:

string siteUrl = "https://site-admin.sharepoint.com";
var authManager = new OfficeDevPnp.Core.AuthenticationManager();
ClientContext ctx = authManager.GetWebLoginClientContext(siteUrl);

I now need to use the following per Microsoft.

  • Microsoft.SharePointOnline.CSOM
  • PnP.Framework

With these new Packages, how am I supposed to create a ClientContext using a AuthenticationManager that supports Multi-Factor Authentication for a Username?

This works so easy with PowerShell using Microsoft.Online.Sharepoint.PowerShell.

Connect-SPOService -Url https://site-admin.sharepoint.com -Credential first.last@site.com
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,300 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 33,641 Reputation points Microsoft Vendor
    2023-02-28T02:21:29.2066667+00:00

    Hi Klein, John,

    Per my test, the latest version of SharePointPnPCoreOnline doesn't contain authManager.GetWebLoginClientContext(siteUrl);. This function has been deleted.

    I will recommend you to use app-only for authentication:

    https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs

    Since you don't want to use app, the only way is to use old version pnpcore. You can follow the steps

    1. Go to Manage NuGet Packet.
    2. choose older version such as 3.25 User's image
    3. Now the authManager.GetWebLoginClientContext(siteUrl) is available

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.


0 additional answers

Sort by: Most helpful