Resolved it by using PostAsync instead of PatchAsync.
User Name and Email association in B2C
JS Arya
46
Reputation points
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.
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)
{
}
}