Can't work out the documentation for adding a PnPCoreAuthentication provider #717
-
I'm working on creating an Azure Function v3 with version 1.5 of PnP.Core.Auth. I would like to create a certification authentication provider. The example you have is for v4 so I'm having to try and figure out how to make this work in v3 and I'm getting an error when trying to create an instance of the auth provider before passing it to the context. The first line below throws an "System.NullReferenceException: 'Object reference not set to an instance of an object.'" (see stack trace below) although I can see that my auth provider is actually registered in the factory->Non-Public Members->Options->Credentials->Configurations var certAuthProvider = _pnpAuthProviderFactory.Create("CertAuth");
using (var pnpContext = _pnpContextFactory.Create(new Uri(webHook.WebUrl), certAuthProvider))
{
...
} I registered the auth provider like this in the Startup.cs file: public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddPnPCore();
builder.Services.AddPnPCoreAuthentication(options =>
{
// Configure certificate based auth
options.Credentials.Configurations.Add("CertAuth", new PnPCoreAuthenticationCredentialConfigurationOptions
{
ClientId = Environment.GetEnvironmentVariable("ClientId"),
TenantId = Environment.GetEnvironmentVariable("TenantId"),
X509Certificate = new PnPCoreAuthenticationX509CertificateOptions
{
Certificate = LoadCertificate(),
}
});
});
} So, i guess my question is, can I not do this in v3? Stack Trace: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
Hi @juliemturner , This sample https://pnp.github.io/pnpcore/demos/Demo.AzureFunction/README.html uses Azure V3 functions with certificate based auth via the // Add and configure PnP Core SDK
builder.Services.AddPnPCore(options =>
{
// Add the base site url
options.Sites.Add("Default", new PnPCoreSiteOptions
{
SiteUrl = Environment.GetEnvironmentVariable("SiteUrl")
});
});
builder.Services.AddPnPCoreAuthentication(options =>
{
// Load the certificate to use
X509Certificate2 cert = LoadCertificate(azureFunctionSettings);
// Configure certificate based auth
options.Credentials.Configurations.Add("CertAuth", new PnPCoreAuthenticationCredentialConfigurationOptions
{
ClientId = azureFunctionSettings.ClientId,
TenantId = azureFunctionSettings.TenantId,
X509Certificate = new PnPCoreAuthenticationX509CertificateOptions
{
Certificate = LoadCertificate(azureFunctionSettings),
}
});
// Connect this auth method to the configured site
options.Sites.Add("Default", new PnPCoreAuthenticationSiteOptions
{
AuthenticationProviderName = "CertAuth",
});
}); |
Beta Was this translation helpful? Give feedback.
Hi @juliemturner ,
This sample https://pnp.github.io/pnpcore/demos/Demo.AzureFunction/README.html uses Azure V3 functions with certificate based auth via the
X509CertificateAuthenticationProvider
provider but your code is almost OK as well, the issue your seeing comes from the fact that you've not connected a site configuration with the auth provider. Can you try something along these lines and see if that works for you: