0

Since Microsoft has switched from basic auth to bearer auth as of 2023, I cannot perform mail reading operations. I am trying to read with GraphServiceClient, but I am getting the below mentioned errors,can you help?

CS0246-->The type or namespace name 'DelegateAuthenticationProvider' could not be found (are you missing a using directive or an assembly reference?) CS1061--> 'MeRequestBuilder' does not contain a definition for 'Request' and no accessible extension method 'Request' accepting a first argument of type 'MeRequestBuilder' could be found (are you missing a using directive or an assembly reference?)

using Microsoft.Graph;   
using System; 
using System.Collections.Generic;  
using System.Linq;  
using System.Threading.Tasks;  
using System.Web;   
using System.Net.Http.Headers;   
using Microsoft.Identity.Client;  
namespace GraphTest.Helpers{   
public class GraphHelper{   
public static async Task<CachedUser> GetUserDetailsAsync(string accessToken){  
var graphClient = new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) =>{


requestMessage.Headers.Authorization =new AuthenticationHeaderValue("Bearer", accessToken);

}));

var user = await graphClient.Me.Request().Select(u =>new{

u.DisplayName,u.Mail,u.UserPrincipalName}).GetAsync();

return new CachedUser {

Avatar = string.Empty,DisplayName = user.DisplayName,Email = string.IsNullOrEmpty(user.Mail)?user.UserPrincipalName : user.Mail};

}

}

}
Tiny Wang
  • 10,423
  • 1
  • 11
  • 29
19sn
  • 11
  • 4
  • You need to provide details on what error you are getting – Glen Scales Jun 08 '23 at 23:35
  • Please share the error and method how you read messages – user2250152 Jun 09 '23 at 05:26
  • Could you pls try to downgrade your `Microsoft.Graph` package from V5.x to V4.x and have a try again? If the downgrade operation brought version conflict with other libraries, just downgrade those libraries as well. By the way, I think you code might be correct when using V4.x library – Tiny Wang Jun 09 '23 at 08:39
  • `'MeRequestBuilder' does not contain a definition for 'Request' and no accessible extension method 'Request' accepting` Here, when we use V5 SDK instead of V4, we would get this `no definition for Request` error – Tiny Wang Jun 09 '23 at 08:55

1 Answers1

0

according to my test, if the access token you used is correct and the nuget package is in a correct version, then your code should work:

enter image description here

In my code snippet, I just generate an access token which contains enough API permission for calling Ms graph API, then it worked for me. The package I used is

    <PackageReference Include="Microsoft.Graph" Version="4.43.0" />
    <PackageReference Include="Microsoft.Identity.Client" Version="4.52.0" />
    <PackageReference Include="Microsoft.Identity.Web" Version="1.26.0" />
    <PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="1.26.0" />
    <PackageReference Include="Microsoft.Identity.Web.UI" Version="1.26.0" />
Tiny Wang
  • 10,423
  • 1
  • 11
  • 29