How to create a client context if legacyauthprotocol is set to false.without changing the legacyauthprotocol using csom.


This a very common issue which we often face when LegacyAuthProtocolsEnabled is set to False in the SPO tenant. It also affects us when we have configured Multi-factor Authentication (MFA) in our tenant.

1) To authenticate to SPO then, we can use the GetWebLoginClientContext method of SharePoint PnP Core library which is available as a nuget package.

So, modify your code as below:

using (var context = new AuthenticationManager().GetWebLoginClientContext("https://tenant.sharepoint.com/sites/test"))
    context.Load(context.Web, web => web.Title);
    Console.WriteLine("Your site title is: " + context.Web.Title);

Add SharePointPnPCoreOnline Nuget package in your CSOM application. After that, you will be able to login to the SPO env. Basically, this will give you a prompt to enter your details in a browser window.

2) You can also use the GetAppOnlyAuthenticatedContext method of PnP CSOM core and pass the client id and secret using which you can get the client context.

After that you can use the code as below:

string siteUrl = "https://tenant.sharepoint.com/sites/test/";
string clientId = "<client-id>";
string clientSecret = "<client-secret>";

using (var clientContext = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl,clientId,clientSecret))
    Web oWebsite = clientContext.Web;
    ListCollection collList = oWebsite.Lists;

References - Authenticate SharePoint using PnP Authentication Manager

Expose on public web your SharePoint Online information

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.