I have an Azure Function which uses PnP.Core.Services
to interact with SharePoint to create a list item. The Azure function is based on .net version 6.0.
I have this startup.cs
:-
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PnP.Core.Auth;
using System.Security.Cryptography.X509Certificates;
[assembly: FunctionsStartup(typeof(FunctionApp2.Startup))]
namespace FunctionApp2
{
class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
var config = builder.GetContext().Configuration;
var azureFunctionSettings = new AzureFunctionSettings();
config.Bind(azureFunctionSettings);
builder.Services.AddPnPCore(options =>
{
options.DisableTelemetry = true;
var authProvider = new X509CertificateAuthenticationProvider(azureFunctionSettings.ClientId,
azureFunctionSettings.TenantId,
StoreName.My,
StoreLocation.CurrentUser,
azureFunctionSettings.CertificateThumbprint);
options.DefaultAuthenticationProvider = authProvider;
options.Sites.Add("Default", new PnP.Core.Services.Builder.Configuration.PnPCoreSiteOptions
{
SiteUrl = azureFunctionSettings.SiteUrl,
AuthenticationProvider = authProvider
});
});
}
}
}
and this Function1.cs
:-
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using PnP.Core.Services;
using PnP.Core.Model.SharePoint;
using System.Collections.Generic;
namespace FunctionApp2
{
public class Function1
{
private readonly IPnPContextFactory pnpContextFactory;
public Function1(IPnPContextFactory pnpContextFactory)
{
this.pnpContextFactory = pnpContextFactory;
}
[FunctionName("Function1")]
public void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
using (var context = pnpContextFactory.Create("Default"))
{
var myList = context.Web.Lists.GetByTitle("SubFolders");
Dictionary<string, object> values = new Dictionary<string, object>
{
{ "Title", System.DateTime.Now }
};
// Use the AddBatch method to add the request to the current batch
myList.Items.AddBatch(values);
context.Execute();
}
}
}
}
and this AzureFunctionSettings.cs
:-
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace FunctionApp2
{
internal class AzureFunctionSettings
{
public string SiteUrl { get; set; }
public string TenantId { get; set; }
public string ClientId { get; set; }
public StoreName CertificateStoreName { get; set; }
public StoreLocation CertificateStoreLocation { get; set; }
public string CertificateThumbprint { get; set; }
}
}
now i am working with a client and they do not allow us to use any open source technologies like PnP. so what i need to do to replace my above PnP code with CSOM code? and is there a CSOM code for .net 6?
Thanks
2
Answers
You can use Microsoft.Identity.Client instead of PnP. I have never used the certificate flow myself but something like this should work:
The code to load your certificate from the store as in the PnP source
If you want to use CSOM and authenticate with a certificate then you need to associate the certificate with your application in Azure. I assume that you have already generated the certificate and your application is registered in Azure Portal.
Open your app in Azure Portal and click on
Manage -> Certificates & secrets
.On tab
Certificates
click onUpload certificate
and upload certificate fromcer
file.Now you need to create a new
Key vaults
resource in Azure portalRemember your
Vault URI
of yourKey vault
resourceClick on
Objects -> Certificates
and then onGenerate/Import
button to import the certificate frompfx
file. Remember the name of the certificate.In the code you need to add
Azure.Identity
,Azure.Security.KeyVault.Certificate
andMicrosoft.Identity.Client
nuget packages.Create
CertificateClient
to download the certificate fromKey vault
and use the certificate inConfidentialClientApplicationBuilder
.Probably you will need to learn more about DefaultAzureCredentail to configure it for using in production.
DefaultAzureCredential
tries different credential types.For development you can choose an account for your apps to authenticate and access Azure resources in
Visual Studio -> Tools -> Options -> Azure Service Authentication
.