User Name and Email association in B2C

JS Arya 46 Reputation points
2022-06-14T05:52:27.877+00:00

We are creating users using Azure AD B2C. It uses User Name signin instead of Email. Users are able to log in once they are registered. But when I am using forgot password flow it gives following error.

211144-2022-06-14-11-03-14-window.png

I searched and found I can use EmailAuthenticationMethod to achieve this. I am using below mentioned code and getting "Exception of type 'Microsoft.Graph.Beta.Models.ODataErrors.ODataError' was thrown." error

public static async Task Main(string[] args)  
        {  
            try  
            {  
                var scopes = new[] { "Directory.AccessAsUser.All", "UserAuthenticationMethod.ReadWrite.All" };  
  
                // Multi-tenant apps can use "common",  
                // single-tenant apps must use the tenant ID from the Azure portal  
                var tenantId = "23*******-*******";  
  
                // Value from app registration";  
                var clientId = "57*******";  
  
                // using Azure.Identity;  
                var options = new TokenCredentialOptions  
                {  
                    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud  
                };  
  
                var userName = "app@*********.com";  
                var password = "*********";  
  
                // https://learn.microsoft.com/dotnet/api/azure.identity.usernamepasswordcredential  
                var userNamePasswordCredential = new UsernamePasswordCredential(  
                    userName, password, tenantId, clientId, options);  
                  
                var graphClient = new Microsoft.Graph.Beta.GraphServiceClient(userNamePasswordCredential, scopes);  
                  
                await graphClient.Users["20*********"]  
                    .Authentication.EmailMethods["3d*****"]                      
                    .PatchAsync(new Microsoft.Graph.Beta.Models.EmailAuthenticationMethod  
                    {  
                        EmailAddress = "email@domain.com"                          
                    });                  
            }  
            catch (Exception ex)  
            {  
  
            }  
        }  
Microsoft Entra External ID
Microsoft Entra External ID
A modern identity solution for securing access to customer, citizen and partner-facing apps and services. It is the converged platform of Azure AD External Identities B2B and B2C. Replaces Azure Active Directory External Identities.
2,987 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JS Arya 46 Reputation points
    2022-06-15T11:57:16.36+00:00

    Resolved it by using PostAsync instead of PatchAsync.

    0 comments No comments

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.