Gill Anderson
Gill Anderson

Reputation: 1

Facing System.Text.Encoding.Web Error while trying to fetch all users with Graph API

I have been exploring with Azure and Graph API. I have created two apps, one is a MVC app and other one is a Azure Function app. When I try graphClient.Users.Request().GetAsync(), I am getting data in MVC App but I am facing error like System.Text.Encoding.Web version=6.0.0.0 with Azure Function. However, in the function app too, I am getting data if I try for a specific user like graphClient.Users[Id].Requests().GetAsync(). Here's my code snippet for initializing and making request.

clientId = Environment.GetEnvironmentVariable("ClientId");
tenantId = Environment.GetEnvironmentVariable("TenantId");
clientSecret = Environment.GetEnvironmentVariable("ClientSecret");

_clientApplication = ConfidentialClientApplicationBuilder.Create(clientId)
        .WithTenantId(tenantId)
        .WithClientSecret(clientSecret)
        .Build();

graphClient = new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) => {

    var authResult = await _clientApplication
        .AcquireTokenForClient(scopes)
        .ExecuteAsync();

    requestMessage.Headers.Authorization =
        new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
    })

var result = await graphClient.Users.Request().Top(100).GetAsync();

My .NET Version is 3.1 and I am using latest Graph API i.e. 4.5.0 and Version of Microsoft.NET.sdk.Functions is 3.1.1. I tried using Filter so as to avoid any internals that might be resulting in my issue but this is of no use. I tried my testcase in other laptop with .net6 and it is working as expected. However, I need to run this with netcore3.1. Please help me resolve this.

Upvotes: 0

Views: 246

Answers (1)

Tiny Wang
Tiny Wang

Reputation: 15906

In your code snippet, I saw you both used Graph SDK and generating an access token. In my humble opinion, since this is an Azure function, you should add User.Read.All application API permission then using client credential flow. Could you pls try my code below?

I created a function with portal experience and code like below:

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Azure.Identity;
using Microsoft.Graph;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    var scopes = new[] { "https://graph.microsoft.com/.default" };
    var tenantId = "xxx.onmicrosoft.com";
    var clientId = "azure_ad_app_id";
    var clientSecret = "client_secret";
    var clientSecretCredential = new ClientSecretCredential(
                    tenantId, clientId, clientSecret);
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    var users = await graphClient.Users.Request().GetAsync();
    return new OkObjectResult("hello");
}

I created a file function.proj in kudo which add the reference. Here the runtime must be netstandard2.0.

<Project Sdk="Microsoft.NET.Sdk">  
    <PropertyGroup>  
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>  
    <ItemGroup>  
        <PackageReference Include="Microsoft.Graph" Version="4.51.0" />
        <PackageReference Include="Azure.Identity" Version="1.8.1" />
    </ItemGroup>  
</Project>  

Upvotes: 0

Related Questions