Reputation: 441
I'm using certificate to get ClientContext in function app(V1) here is code:
ClientContext newClientContext;
try
{
newClientContext = new AuthenticationManager().GetAzureADAppOnlyAuthenticatedContext(SiteUrl, appId, tenant, certificate);
newClientContext.ExecuteQuery();
return newClientContext;
}
catch (Exception ex)
{
newClientContext = null;
if (_logHelper != null)
{
_logHelper.writeLog("GetAzureADContextError:"+ex.Message, TraceLevel.Error, ex);
}
return null;
}
It works pretty well in local visual studio env,but get failed information after deploy to app services
Could not load file or assembly 'Microsoft.IdentityModel.Clients.ActiveDirectory, Version=2.29.0.1078, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
Upvotes: 2
Views: 3500
Reputation: 15754
Regarding the error, the SDK SharePointPnPCoreOnline
requests Microsoft.IdentityModel.Clients.ActiveDirectory 2.29.0.1078
, but the sdk Microsoft.Azure.Services.AppAuthentication
needs Microsoft.IdentityModel.Clients.ActiveDirectory
version higher than 3.14.2
. According to the situation, I suggest you use key vault reference in your Azure Function. After we do that, we can remove sdk Microsoft.Azure.Services.AppAuthentication
and downgrade sdk Microsoft.IdentityModel.Clients.ActiveDirectory
to version 2.29.
The detailed steps are as below.
Create an access policy in Key Vault for the Azure Function application identity
Get the certificate url
@Microsoft.KeyVault(SecretUri=<your url>)
Microsoft.Azure.Services.AppAuthentication
and downgrade sdk Microsoft.IdentityModel.Clients.ActiveDirectory
var s = Environment.GetEnvironmentVariable("cert");
var cert = new X509Certificate2(Convert.FromBase64String(s),
(string)null,
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
try
{
using (var cc = new OfficeDevPnP.Core.AuthenticationManager().GetAzureADAppOnlyAuthenticatedContext(
SiteUrl, appId, tenant,
cert))
{
cc.Load(cc.Web, p => p.Title);
cc.ExecuteQuery();
log.Info("Via PnP, we have site: " + cc.Web.Title);
};
}
catch (Exception ex) {
log.Info(ex.Message);
}
Upvotes: 1