Problem extract data using Microsoft Graph - C# .NET

OdMa 0 Reputation points
2023-03-21T10:41:54.9733333+00:00

I'm building a program that login through the Azure AD with the Microsoft account. After logging in, I need to extract more data about the user, in order to know what permissions I need to give him, so I thought of using Microsoft Graph, but I can't do it... I went through your guide and I get an error on some of the functions even though I downloaded the package needed for use.

Asking for an explanation and support - this program written in C# .NET.

Attached here is the error and the code itself.
תמונה של WhatsApp‏ 2023-03-21 בשעה 12.00.39

תמונה של WhatsApp‏ 2023-03-21 בשעה 12.00.40

Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Siddharth Gautam 860 Reputation points
    2023-03-22T00:09:24.7+00:00

    Hello OdMa,

    Thanks for posting!

    As per my analysis, could you please update the line 39 in your code with the below line of code:

    var graphClient = new GraphServiceClient(requestAdapter);
    var result = await graphClient.Users.GetAsync();
    

    Please refer This link for same.

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote. If you have any further questions about this answer, please click Comment.


  2. CarlZhao-MSFT 46,371 Reputation points
    2023-03-22T10:29:47.11+00:00

    Hi @OdMa

    Microsoft Graph .NET SDK v5 has deleted the Request() method. If you want to get the information of the logged-in user, you should use auth code flow or ROPC flow for authentication.

    Please refer to my sample code:

    using Azure.Identity; 
    using Microsoft.Graph;
    
    var scopes = new[] { "User.Read.All" };
    
    var tenantId = "{tenant id}";
    
    // Values from app registration
    var clientId = "{client id}";
    var clientSecret = "{client secret}";
    
    // For authorization code flow, the user signs into the Microsoft
    // identity platform, and the browser is redirected back to your app
    // with an authorization code in the query parameters
    var authorizationCode = "{auth code}";
    
    // using Azure.Identity;
    var options = new TokenCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
    };
    
    // https://learn.microsoft.com/dotnet/api/azure.identity.authorizationcodecredential
    var authCodeCredential = new AuthorizationCodeCredential(
        tenantId, clientId, clientSecret, authorizationCode, options);
    
    var graphClient = new GraphServiceClient(authCodeCredential, scopes);
    
    var result = await graphClient.Me.GetAsync();
    
    
    Console.WriteLine("id:" + result.Id + "\r\n" + "displayName:" + result.DisplayName);
    
    

    User's image

    Dependency package:

    User's image

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.