Kevin YANG
Kevin YANG

Reputation: 441

Could not load file or assembly 'Microsoft.IdentityModel.Clients.ActiveDirectory when calling GetAzureADAppOnlyAuthenticatedContext

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;
            }

and my package is : package version list

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

Answers (1)

Hury Shen
Hury Shen

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.

  1. Configure MSI for Azure Function

  2. Create an access policy in Key Vault for the Azure Function application identity

  3. Get the certificate url

enter image description here enter image description here

  1. Save the url in the Azure function Application settings. Its formate should be like @Microsoft.KeyVault(SecretUri=<your url>)

enter image description here

  1. Remove SDK Microsoft.Azure.Services.AppAuthentication and downgrade sdk Microsoft.IdentityModel.Clients.ActiveDirectory
  2. Update code
 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);
            }

enter image description here

Upvotes: 1

Related Questions