I can connect to a SharePoint Online Web using the PnP Framework, but am unable to do so using PnP SDK Core using the same app registration and certificate.
What am I doing wrong?
Here is my PnP Framework code:
var clientId = "..";
var tenantId = "..";
var siteUrl = "..";
X509Certificate2 certificate = GetKeyVaultCertificate(vaultName, certificateName);
var authManager = AuthenticationManager.CreateWithCertificate(clientId, certificate, tenantId);
using (var context = await authManager.GetContextAsync(siteUrl))
{
var web = context.Web;
context.Load(web, w => w.Id, w => w.Title);
await context.ExecuteQueryAsync();
Console.WriteLine($"{web.Id} - {web.Title}");
}
Here is my PnP SDK Code:
var clientId = "..";
var tenantId = "..";
var siteUrl = "..";
var certificate = GetKeyVaultCertificate(vaultName, certificateName);
var host = Host.CreateDefaultBuilder()
.ConfigureServices((hostingContext, services) =>
{
services.AddPnPCore(options =>
{
options.Sites.Add("DefaultSite", new PnPCoreSiteOptions
{
SiteUrl = siteUrl
});
});
services.AddPnPCoreAuthentication( options =>
{
options.Credentials.Configurations.Add("x509certificate", new PnPCoreAuthenticationCredentialConfigurationOptions
{
ClientId = clientId,
TenantId = tenantId,
X509Certificate = new PnPCoreAuthenticationX509CertificateOptions
{
Certificate = certificate
}
});
options.Credentials.DefaultConfiguration = "x509certificate";
options.Sites.Add("DefaultSite", new PnPCoreAuthenticationSiteOptions
{
AuthenticationProviderName = "x509certificate"
});
});
})
.UseConsoleLifetime()
.Build();
await host.StartAsync();
using (var scope = host.Services.CreateScope())
{
var pnpContextFactory = scope.ServiceProvider.GetRequiredService<IPnPContextFactory>();
using (var context = await pnpContextFactory.CreateAsync("BccSite"))
{
var web = await context.Web.GetAsync(p => p.Title);
Console.WriteLine($"Title: {web.Title}");
}
}
host.Dispose();
The exception occurs on the GetAsync line:
PnP.Core.MicrosoftGraphServiceException
HResult=0x80131500
Message=Microsoft Graph service exception