I have the following console application to connect to my online site:-
using Microsoft.SharePoint.Client;
using System;
using System.Linq;
using System.Security;
using System.Linq;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string siteCollectionUrl = "https://***.sharepoint.com";
string userName = "***@***.com";
string password = "T*****";
Program obj = new Program();
try
{
obj.ConnectToSharePointOnline(siteCollectionUrl, userName, password);
}
catch (Exception ex)
{
string msg = ex.Message.ToString();
}
}
public void ConnectToSharePointOnline(string siteCollUrl, string userName, string password)
{
//Namespace: It belongs to Microsoft.SharePoint.Client
ClientContext ctx = new ClientContext(siteCollUrl);
// Namespace: It belongs to System.Security
SecureString secureString = new SecureString();
password.ToList().ForEach(secureString.AppendChar);
// Namespace: It belongs to Microsoft.SharePoint.Client
ctx.Credentials = new SharePointOnlineCredentials(userName, secureString);
// Namespace: It belongs to Microsoft.SharePoint.Client
Site mySite = ctx.Site;
ctx.Load(mySite);
ctx.ExecuteQuery();
Console.WriteLine(mySite.Url.ToString());
}
}
}
now the user i am trying to login with have 2 factor authentication enabled. so when i run the application, i got this exception "The sign-in name or password does not match one in the Microsoft account system" although i am passing valid username and password .. any idea?