Hi @Ariel M ,
To help you better, can you share more information on this issue so that I can reproduce the issue? I was able to use the code you provided successfully without any errors.
If you want to connect to SharePoint Online using CSOM, you can use the following code:
class Program
{
private class Configuration
{
public static string ServiceSiteUrl = "https://xxxxx.sharepoint.com/sites/xxx";
public static string ServiceUserName = "xxxx@xxxx.onmicrosoft.com";
public static string ServicePassword = "xxxxxx";
}
static ClientContext GetonlineContext()
{
var securePassword = new SecureString();
foreach (char c in Configuration.ServicePassword)
{
securePassword.AppendChar(c);
}
var onlineCredentials = new SharePointOnlineCredentials(Configuration.ServiceUserName, securePassword);
var context = new ClientContext(Configuration.ServiceSiteUrl);
context.Credentials = onlineCredentials;
return context;
}
static void Main(string[] args)
{
var ClientContext = GetonlineContext();
Web web = ClientContext.Web;
ClientContext.Load(web);
ClientContext.ExecuteQuery();
Debug.WriteLine(web.Title);
}
}
If you want to connect to a SharePoint site with MFA using CSOM, you can use the following code:
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);
}
More information for reference: Sharepoint: Connect to site with MFA enabled using CSOM and PnP PowerShell
Hope it can help you. Thanks for your understanding.
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.