I heard that MS will use only "modern authentication" later. We have some apps on our servers that connect to SharePoint Online and use CSOM. Will this code-snippet continue to work, or we need to create some Azure Apps or any additional steps?..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

using Microsoft.SharePoint.Client; 

namespace ClientModelConsoleApp
    class Program
        static void Main(string[] args)
            string siteUrl = "https://server.sharepoint.com/sites/sitename/";
            string username = "xxx";
            string password = "yyy";

           using (ClientContext clientContext = CreateClientContext(siteUrl, username, password))
                ListCollection lists = clientContext.Web.Lists;

                foreach (List oList in lists)
                    Console.WriteLine("Title: {0} Created: {1}", oList.Title, oList.Created.ToString());

        private static ClientContext CreateClientContext(string siteUrl, string username, string password)
            ClientContext context = new ClientContext(siteUrl);
            var securePassword = new SecureString();
            foreach (var chr in password) securePassword.AppendChar(chr);
            context.Credentials = new SharePointOnlineCredentials(username, securePassword);
            return context;

If Multi-factor authentication (MFA) is activated in your environment, this code will not work anymore. You have to use SharePointPnPCoreOnline: https://www.c-sharpcorner.com/blogs/using-csom-to-connect-to-a-sharepoint-site-with-multi-factor-authentication-enabled

Another workaround is using App Only autentication: https://www.c-sharpcorner.com/article/connect-to-sharepoint-online-site-with-app-only-authentication/

  • Thank You much for answer, @michael-han-msft! Will try App-Only client/secret – Gennady G Oct 22 '20 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.