Here I suggest a code that I use in my developments:
using Microsoft.Graph;
using Microsoft.Identity.Client;
using System;
using System.Threading.Tasks;
public class B2CUserSample
{
private static string clientId = "Client_ID";
private static string tenantId = "Tenant_ID";
private static string clientSecret = "Client_Secret";
public static async Task Main(string[] args)
{
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority($"https://login.microsoftonline.com/{tenantId}")
.Build();
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
AuthenticationResult authenticationResult = await confidentialClientApplication.AcquireTokenForClient(scopes).ExecuteAsync();
GraphServiceClient graphClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
{
requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken);
return Task.CompletedTask;
}));
string userEmailAddress = "******@youremail.com";
string oneTimePasscodeEmailTemplateId = "Your_Email_Template_Id";
Invitation invitation = new Invitation
{
InvitedUserEmailAddress = userEmailAddress,
InvitedUserType = "Consumer",
InviteRedirectUrl = "https://yourredirecturl.com",
InvitedUserMessageInfo = new InvitedUserMessageInfo
{
CustomizedMessageBody = $"Your Body Message: {{OTP}}",
AdditionalData = new System.Collections.Generic.Dictionary<string, object>
{
{ "otpEmailTemplateId", oneTimePasscodeEmailTemplateId }
}
}
};
try
{
await graphClient.Invitations.Request().AddAsync(invitation);
Console.WriteLine("Invitation sent successfully.");
}
catch (Exception ex)
{
Console.WriteLine("Error sending invitation: " + ex.Message);
}
}
}
Regards,