SharePoint Online authenitication from console application

Sachin Prakash 1 Reputation point

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

Azure Active Directory
Azure Active Directory
An Azure enterprise identity service that provides single sign-on and multi-factor authentication.
16,655 questions
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
8,089 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Baker Kong-MSFT 3,786 Reputation points

    Hi @Sachin Prakash ,

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


    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 = "";  
                //var pwd = "userpassword";  
                var pwd = "gnckrrjmdwvbywtr";  //this is app password!  
                var username = "";  
                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;  
                Console.WriteLine($"web title: {web.Title}");  

    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.