Question

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;
                clientContext.Load(lists);
                clientContext.ExecuteQuery();

                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;
        }
    }
}
Was it helpful?

Solution

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top