Logo
banner

Blogs

Secure Authentication of SharePoint with PnP Framework with C#(Code)

, April 30, 2021 2223 Views

For a while, we had been using a NuGet Package named SharePointPnPCoreOnline(as shown in below image), which can be used for authentication to SharePoint and other SharePoint Client Side Object Model (CSOM) tasks for managing SharePoint Online Data. This is what we used in our previous blog posts part1 and part2 to connect using clientid/client secret.

But now this Package is retired and Microsoft Recommends using an alternative Package, namely PnP Framework, as highlighted in above picture.

So now we will be working with PnP Framework, but first what is PnP? PnP stands for Patterns and Practices, it is a Microsoft Community initiative headed by Microsoft Engineering to share implementation practices for Microsoft 365 products like SharePoint, OneDrive, Microsoft Teams, etc. One of the projects of PnP is PnP Framework which has lots of tools for working with Microsoft 365, SharePoint in this case.

After installing PnP Framework into our C# Project from NuGet Package we are ready to use its methods and classes into our code using PnP.Framework namespace into our code.

Below is a C# code snippet, which we are using to authenticate client with SharePoint with the method given in the package, which is secure and can be used as an alternative to the retired(deprecated) Package. The other parts mentioned in the earlier blogs remain the same.

using PnP.Framework;
using System;
namespace TestPnPFramework
{
  class Program
  {
    static void Main(string[] args)
    {
      string siteUrl = "https://inkeysolutionstraining.com/sites/demo"; //Sharepoint URL
      string clientId = "27027062-04fb-4818-a928-f14658a9fe90"; //Please note this is a sample Client Id
      string clientSecret = "UUw3Ci5j/OaKuNytPFxOXzg8bwB+z5FpK9U+qJFn2KY="; // Please note this is a sample Client Secret

      using (var clientContext = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, clientId, clientSecret))
      {
        clientContext.Load(clientContext.Web, p => p.Title);
        clientContext.ExecuteQuery();
        Console.WriteLine(clientContext.Web.Title);

      };
    }
  }
}

As shown in code above, GetACSAppOnlyContext method of PnP Framework is being used to authenticate with site URL, client id and client secret as parameters, to securely authenticate and returns client context for further operation.

Similar to this method there are many others available for many different tasks and they keep increasing, as the PnP Framework is community drive and can be found on GitHub.

Hope this helps

 


ATM Inspection PowerApp to ease ATM inspection and report generation process.
https://www.inkeysolutions.com/microsoft-power-platform/power-app/atm-inspection

Insert data into Many-to-Many relationship in Dynamics CRM very easily & quickly, using the Drag and drop listbox.
http://www.inkeysolutions.com/what-we-do/dynamicscrmaddons/drag-and-drop-listbox

Comply your Lead, Contact, and User entities of D365 CRM with GDPR compliance using the GDPR add-on.
https://www.inkeysolutions.com/microsoft-dynamics-365/dynamicscrmaddons/gdpr

Create a personal / system view in Dynamics CRM with all the fields on the form/s which you select for a particular entity using the View Creator.
http://www.inkeysolutions.com/what-we-do/dynamicscrmaddons/view-creator

mm

Inkey

INKEY is your solution partner.
Our focus is to deliver you in-time intelligent innovative solutions ("key") for the problems in hand. Maintaining a quality standard right from the inception of a project is our top most priority.

Our team of talented professionals will execute your projects with dedication and excellence. We take ownership and accountability for the effort that goes into meeting our client’s needs.

Years of experience and proven success of delivering innovative custom solutions.

More posts by

Leave a Reply

Your email address will not be published. Required fields are marked *