SharePoint Online Authentication: The identity has not been authenticated.

Ariel M 51 Reputation points
2022-10-18T16:58:42.28+00:00
string siteCollectionUrl = "https://tenant.sharepoint.com/";        
string userName = "user@companyname.onmicrosoft.com";        
string password = "XXXXXX";        
    
// Namespace: Microsoft.SharePoint.Client        
ClientContext ctx = new ClientContext(siteCollectionUrl);       
    
// Namespace: System.Security      
SecureString secureString = new SecureString();         
password.ToList().ForEach(secureString.AppendChar);        
    
// Namespace: Microsoft.SharePoint.Client        
ctx.Credentials = new SharePointOnlineCredentials(userName, secureString);       
    
// Namespace: Microsoft.SharePoint.Client        
Site site = ctx.Site;       
    
ctx.Load(site);        
ctx.ExecuteQuery();     
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,059 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,141 Reputation points
    2022-10-19T03:05:23.477+00:00

    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.