2

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;
        }
    }
}

1 Answer 1

3

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/

2
  • 1
    SharePointPnPCoreOnline is only available within .NET Framework which is an end of life product. .NET Modern auth is preferred
    – David Ford
    Jul 14, 2021 at 12:02
  • @DavidFord Nowadays you would need to use PnP.Core and PnP.Framework to perform authentication. Don't have a great resource, but here's one option (archived link as it now redirects to some spam or something): web.archive.org/web/20210727154547/https://… see also github.com/pnp/pnpframework
    – TylerH
    Oct 13 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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