SharePoint Online authenitication from console application

Sachin Prakash 1 Reputation point
2020-12-23T05:28:37.607+00:00

Hi,
How to authenticate to SharePoint Online with Service Account user name and passward using .NET Application (By Modern authentication and not using client id Secret )
Please suggest on the best appraoach to achieve the same

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,607 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,454 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Baker Kong-MSFT 3,791 Reputation points
    2020-12-23T08:05:16.66+00:00

    Hi @Sachin Prakash ,

    You could consider using App password to overcome the MFA etc.

    50802-image.png

    This password will act as the same role as user password but won't require MFA procedure (dialogue popup).

    static void Main(string[] args)  
            {             
                string SiteUrl = "https://abc.sharepoint.com/sites/s01";  
      
                //var pwd = "userpassword";  
                var pwd = "gnckrrjmdwvbywtr";  //this is app password!  
                var username = "support@abc.onmicrosoft.com";  
      
                Console.WriteLine("Hello World!");  
      
                ClientContext context = new ClientContext(SiteUrl);  
      
                SecureString securestring = new SecureString();  
                pwd.ToCharArray().ToList().ForEach(s => securestring.AppendChar(s));  
      
                context.Credentials = new SharePointOnlineCredentials(username, securestring);  
                  
      
                var web = context.Web;  
                context.Load(web);  
                context.ExecuteQuery();  
      
                Console.WriteLine($"web title: {web.Title}");  
                  
                Console.ReadKey();           
            }  
    

    Best Regards,
    Baker Kong


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    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.