错误System.TypeLoadException:'AuthenticateRequestAsync'如何解决这个问题?

时间:2018-04-23 09:46:07

标签: c# azure visual-studio-2017 .net-assembly

您好我有这个错误:

Gravité Code    Description Projet  Fichier Ligne   État de la suppression
Erreur      System.TypeLoadException: La méthode 'AuthenticateRequestAsync' du type 'AzureAuthenGraph.AzureAuthenticationProvider' de l'assembly 'AzureAuthenGraph, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' n'a pas d'implémentation.
   à System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
   à System.Reflection.RuntimeAssembly.GetExportedTypes()
   à MakeFunctionJson.FunctionJsonConverter.TryGenerateFunctionJsons()
   à MakeFunctionJson.FunctionJsonConverter.TryRun()
Error generating functions metadata
    AzureAuthenGraph    C:\Users\*****\.nuget\packages\microsoft.net.sdk.functions\1.0.13\build\netstandard1.0\Microsoft.NET.Sdk.Functions.Build.targets    39  

我的代码:

    public async Task<IGraphServiceUsersCollectionPage> GetUsersByLastName(string lastname)
    {
        GraphServiceClient graphClient = new GraphServiceClient(new AzureAuthenticationProvider());
        string filter = String.Format("startswith(surname, '{0}')", lastname);
        IGraphServiceUsersCollectionPage users = await graphClient.Users.Request().Filter(filter).GetAsync(); 
        return users;
    }
}
public class AzureAuthenticationProvider : IAuthenticationProvider
{
    private string _azureDomain = "myDevDom.onmicrosoft.com";

    public async Task AuthenticateRequestAsync(HttpRequestMessage request)
    {
        try
        {
            string clientId = "2b823c67-1b0d-4a10-a9e1-737142516f5q";
            string clientSecret = "xxxxxx";

            AuthenticationContext authContext = new AuthenticationContext("https://login.windows.net/" + _azureDomain + "/oauth2/token");

            ClientCredential credentials = new ClientCredential(clientId, clientSecret);

            AuthenticationResult authResult = await authContext.AcquireTokenAsync("https://graph.microsoft.com/", credentials);

            request.Headers.Add("Authorization", "Bearer " + authResult.AccessToken);
        }
        catch (Exception ex)
        {
        }
    }

}

}

我的项目是带有http triger的Azure functionApp版本1.0。

我已经尝试删除bin文件夹并重建它并不起作用。 我不知道如何解决这个问题的任何想法?

Bests Regards, RobinLotz

1 个答案:

答案 0 :(得分:0)

  

我已经尝试删除bin文件夹并重建它并不起作用。我不知道如何解决这个问题的任何想法?

如果我使用您提到的代码,我也可以重现您提到的问题。

如果我们要初始化 GraphServiceClient ,我们可以使用以下代码来执行此操作。它正确地在我身边工作。

 string authority = "https://login.microsoftonline.com/{0}";
 string graphResourceId = "https://graph.microsoft.com";
 string tenantId = "tenant Id ";
 string clientId = "client id";
 string secret = "secret key";
 authority = String.Format(authority, tenantId);
 AuthenticationContext authContext = new AuthenticationContext(authority);
 var accessToken = authContext.AcquireTokenAsync(graphResourceId, new ClientCredential(clientId, secret)).Result.AccessToken;
 var graphserviceClient = new GraphServiceClient(
        new DelegateAuthenticationProvider(
                    requestMessage =>
                    {
                        requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);

                        return Task.FromResult(0);
                    }));

测试结果

enter image description here