📁 Reading SharePoint Authentication Parameters from a JSON File in .NET with PnP Core SDK

Author: Edvaldo Guimarães\ Category: Microsoft 365 Development / C# / SharePoint\ Tags: .NET 6, PnP Core SDK, MSAL, SharePoint Online, Configuration


🧠 Introduction

In this article, we’ll enhance a .NET console application that connects to SharePoint Online using the PnP Core SDK and MSAL interactive login. Instead of hardcoding authentication parameters, we’ll load them from a local JSON configuration file, making the app more flexible and secure.


🛠️ Why Use a JSON Configuration File?

  • Keeps sensitive data out of the source code
  • Makes it easier to switch between environments (dev, test, prod)
  • Improves maintainability and version control

🧾 Sample config.json

Create a file named config.json in the root of your project with the following structure:

{
  "ClientId": "YOUR_CLIENT_ID",
  "TenantId": "YOUR_TENANT_ID",
  "SiteUrl": "https://YOUR_DOMAIN.sharepoint.com/sites/YOUR_SITE"
}


🚀 Final Working Code

using System;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PnP.Core.Auth;
using PnP.Core.Services;

class Config
{
    public string ClientId { get; set; }
    public string TenantId { get; set; }
    public string SiteUrl { get; set; }
}

class Program
{
    static async Task Main(string[] args)
    {
        var services = new ServiceCollection();

        // Load configuration from JSON file
        string configJson = File.ReadAllText("config.json");
        var config = JsonSerializer.Deserialize<Config>(configJson);

        // Register PnP Core SDK services
        services.AddPnPCore();
        services.AddPnPCoreAuthentication();

        // Register interactive authentication provider
        services.AddScoped<IAuthenticationProvider>(sp =>
            new InteractiveAuthenticationProvider(
                config.ClientId,
                config.TenantId,
                new Uri("http://localhost")
            )
        );

        var serviceProvider = services.BuildServiceProvider();
        var contextFactory = serviceProvider.GetRequiredService<IPnPContextFactory>();

        using var context = await contextFactory.CreateAsync(config.SiteUrl);

        Console.WriteLine("✅ Successfully connected to SharePoint!");
    }
}


✅ What This Code Does

  • Defines a Config class to map JSON fields
  • Reads config.json using System.Text.Json
  • Injects the values into the authentication provider
  • Connects to SharePoint Online using PnP Core SDK

🔄 What’s Next?

In the next article, we’ll:

  • Scan a local directory for files modified in the last hour
  • Upload those files as attachments to a SharePoint list item

This will turn your app into a practical automation tool for document tracking and integration with Microsoft 365.


📌 Conclusion

Reading configuration from a JSON file is a best practice that improves flexibility and security. Combined with the PnP Core SDK and MSAL, this approach sets a solid foundation for building scalable SharePoint automation tools in .NET.


Edvaldo Guimrães Filho Avatar

Published by