pnp / pnpframework

PnP Framework is a .NET library targeting Microsoft 365 containing the PnP Provisioning engine and a ton of other useful extensions
https://pnp.github.io/pnpframework/
MIT License
205 stars 143 forks source link

`HubSiteUrl` property does not work when applying provisioning template #1024

Open JakeStanger opened 4 months ago

JakeStanger commented 4 months ago

When applying a site-level provisioning template, the HubSiteUrl property always fails with a warning, which is possibly due to a bugged implementation.

Before attempting to make the association, the framework does the following check (TenantExtensions.IsCurrentUserTenantAdmin --> IsCurrentUserTenantAdminViaSPO):

var adminSiteUrl = clientContext.Web.GetTenantAdministrationUrl();
try
{
    // Connect to the Admin Site
    using (var adminContext = clientContext.Clone(adminSiteUrl))
    {
        // Do something with the Tenant Admin Context
        Tenant tenant = new Tenant(adminContext);
        tenant.EnsureProperty(t => t.RootSiteUrl);

        // If we've got access to the tenant admin context, 
        // it means that the currently connecte user is an admin
        return (true);
    }
}
catch
{
    // In case of any connection exception, the user is not an admin
    return (false);
}

This never succeeds and fails 401, falling into the catch block. To me it makes sense that it would fail, because the authentication is against a SharePoint site. Attempting to use that same token for the admin URL won't work because it is a different scope.

To resolve, this check would need to be replaced with one that obtains an admin token correctly.

The issue occurs for both application and delegated permissions.

Originally discussed in #897