SharePoint Online Authentication: The identity has not been authenticated.

Ariel M 51 Reputation points
string siteCollectionUrl = "";        
string userName = "";        
string password = "XXXXXX";        
// Namespace: Microsoft.SharePoint.Client        
ClientContext ctx = new ClientContext(siteCollectionUrl);       
// Namespace: System.Security      
SecureString secureString = new SecureString();         
// Namespace: Microsoft.SharePoint.Client        
ctx.Credentials = new SharePointOnlineCredentials(userName, secureString);       
// Namespace: Microsoft.SharePoint.Client        
Site site = ctx.Site;       
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

    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 = "";  
                public static string ServiceUserName = "";  
                public static string ServicePassword = "xxxxxx";  
            static ClientContext GetonlineContext()  
                var securePassword = new SecureString();  
                foreach (char c in Configuration.ServicePassword)  
                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;  

    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>";   
       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);   
       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.