0

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

1

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);
    context.ExecuteQuery();
    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;
    clientContext.Load(collList);
    clientContext.ExecuteQuery();
}

References - Authenticate SharePoint using PnP Authentication Manager

Expose on public web your SharePoint Online information

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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