How to authenticate with a tenant in PnP

Copper Contributor

I need to create a method to do some operations in SPO, lets say, create a modern site, but I need it  in a REST API, that will be consumed later by a frontend angular or react app.


I found this sample:


var targetTenantUrl = "https://[tenant]";

using (var context = new ClientContext(targetTenantUrl))
    context.Credentials = OfficeDevPnP.Core.Utilities.CredentialManager.GetSharePointOnlineCredential("[Name-of-Your-Credentials]");

    // Create new "modern" communication site at the url https://[tenant]
    var communicationContext = await context.CreateSiteAsync(new CommunicationSiteCollectionCreationInformation {
        Title = "title", // Mandatory
        Description = "description", // Mandatory
        Lcid = 1033, // Mandatory
        AllowFileSharingForGuestUsers = false, // Optional
        Classification = "classification", // Optional
        SiteDesign = CommunicationSiteDesign.Topic, // Mandatory
        Url = "https://[tenant]", // Mandatory
    communicationContext.Load(communicationContext.Web, w => w.Url);

The issue with this sample is that it takes a Credential as a parameter which is saved INTO the windows credentials manager.


On the context of an Azure WebApp, I suppose this wont work, and this sample would only work on a console app.


What are other alternatives to get a Sharepoint Context? I know App-Only doesnt work (already tested it)


0 Replies