0

How do you authenticate to SharePoint Online with C#? Error msg: The identity has not been authenticated.

using (ClientContext clientContext = new
  ClientContext("https://yoursite.sharepoint.com/")) {
    var passWord = new SecureString();

    foreach (char c in "yourpassword".ToCharArray()) passWord.AppendChar(c);

    clientContext.Credentials = new SharePointOnlineCredentials("loginname@yoursite.onmicrosoft.com",
passWord);

    Web web = clientContext.Web;

    clientContext.Load(web);

    clientContext.ExecuteQuery();

    Console.WriteLine(web.Title);

    Console.ReadLine(); 
}
Azhar Khan
  • 3,829
  • 11
  • 26
  • 32

1 Answers1

0

If Multi-factor Authentication (MFA) is enabled for user account, try using App password for login instead of user account password.

Reference: The sign-in name or password does not match one in the Microsoft account system

OR

Use the GetWebLoginClientContext method of SharePoint PnP Core library:

AuthenticationManager authManager = new AuthenticationManager();
using (var context = authManager.GetWebLoginClientContext(URI))
{
    context.Load(context.Web, web => web.Title);
    context.ExecuteQuery();
    Console.WriteLine("Your site title is: " + context.Web.Title);
}

For more information, check:

  1. C# Programmatically Authenticate SharePoint Online account enabled with MFA.
  2. Authentication to Sharepoint Online with CSOM
  3. Authenticate to SharePoint Online C#
Ganesh Sanap
  • 1,386
  • 1
  • 8
  • 18