get user email from adb2c using graph api

Miguel 280 Reputation points
2023-07-18T22:49:32.17+00:00

Im using


  var scopes = new[] { "https://graph.microsoft.com/.default" };

  var clientSecretCredential = new ClientSecretCredential(tenantId, appId, clientSecret);
  var graphClient = new GraphServiceClient(clientSecretCredential, scopes);


how to change it to get the user email?thx

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,854 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
22,967 questions
0 comments No comments
{count} votes

Accepted answer
  1. Alfredo Revilla - Upwork Top Talent | IAM SWE SWA 27,491 Reputation points
    2023-08-01T09:56:40.5233333+00:00

    Hello @Miguel , in order to get the user email for an Azure AD B2C consumer user account you can do use the following .NET code. It will display the emailAddress used for sign-in (local accounts) or the first otherMails address (for federated accounts).

    var clientSecretCredential = new ClientSecretCredential(tenantId, appId, clientSecret);
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    var result = await graphClient.Users["id"].GetAsync(c => c.QueryParameters.Select = new[] { "identities" });
    Console.WriteLine(result?.Identities?.Find(o => o.SignInType == "emailAddress")?.IssuerAssignedId ?? result?.OtherMails?.FirstOrDefault());
    

    Let us know if you need additional assistance. If the answer was helpful, please accept it and rate it so that others facing a similar issue can easily find a solution.

    0 comments No comments

0 additional answers

Sort by: Most helpful

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.