我有一个Azure函数,它使用PnP.Core.Services
与SharePoint交互以创建列表项。Azure函数基于. net版本6.0。
我有这个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
});
});
}
}
}
和这个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();
}
}
}
}
和这个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; }
}
}
现在我正在和一个客户一起工作,他们不允许我们使用任何开源技术,比如PnP。2那么我需要做什么来用CSOM代码替换我上面的PnP代码呢?3有没有一个CSOM代码用于. net 6?
谢谢
2条答案
按热度按时间cnjp1d6j1#
您可以使用Microsoft.Identity.Client代替PnP。我自己从未使用过证书流,但类似以下的东西应该可以工作:
用于从存储区加载证书的代码,如PnP source
flseospp2#
如果你要使用CSOM并使用证书进行身份验证,则需要将证书与Azure中的应用程序关联。我假设你已生成证书,并且你的应用程序已在Azure门户中注册。
在Azure门户中打开你的应用,然后单击
Manage -> Certificates & secrets
。在选项卡
Certificates
上,单击Upload certificate
并从cer
文件上传证书。现在,您需要在Azure门户中创建新的
Key vaults
资源记住
Key vault
资源的
Vault URI
单击
Objects -> Certificates
,然后单击Generate/Import
按钮从pfx
文件导入证书。记住证书的名称。在代码中,您需要添加
Azure.Identity
、Azure.Security.KeyVault.Certificate
和Microsoft.Identity.Client
nuget包。创建
CertificateClient
以从Key vault
下载证书并在ConfidentialClientApplicationBuilder
中使用该证书。你可能需要了解有关DefaultAzureCredentail的详细信息,才能将其配置为在生产中使用。
DefaultAzureCredential
尝试不同的凭据类型。对于开发,你可以为应用选择一个帐户,以便在Visual Studio -> Tools -> Options -> Azure Service Authentication
中进行身份验证和访问Azure资源。