The system should implement a producer that sends messages to a broker when a new customer signs up or logs in. The message should contain relevant information about the customer's journey (e.g., which landing page they visited, whether they signed up or logged in, etc.). For this I need to implement an Azure Function HTTP trigger where I need to inform about a new Azure AD B2C user registration or sign in to an App registered(Landing Page).
Here I added my function URL to the regitered app but I'm not sure how this should work, the intention is when a user Sign in in this Landing page for example, to receive like an event in my azure function with some data about the user like I explain before, to create the message I will send(this part is covered)
I've made a general implementation of the azure function, the main situation is how to get the data about the user sign in or sign up, after that I can manage with this next implementation and how to access to the data, for now I can't get anything from the registered app(Landing Page). I'm running this Landing page locally so I can my self sign in to try if I get some data but still nothing coming. Here is the azure function implementation:
public class MessageBrokerProducerFunction
{
private readonly IPublishEndpoint _publishEndpoint;
public MessageBrokerProducerFunction(IPublishEndpoint publishEndpoint)
{
_publishEndpoint = publishEndpoint;
}
[FunctionName("B2CCallbackFunction")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
//Access token
//string accessToken = data.access_token;
var responseMessage = string.IsNullOrEmpty(data)
? "This HTTP triggered function executed with empty response."
: $"This is the data from the request: {data}. This HTTP triggered function executed successfully.";
//Set the message according to the data from request
var message = new MessageCustomer(Guid.NewGuid(), data, data, data, data);
return new OkObjectResult(responseMessage);
}
private async Task PublishMessageToServiceBus(object message, ILogger logger, string sessionId)
{
try
{
await _publishEndpoint.Publish(message, context => context.SetSessionId(sessionId));
logger.LogInformation($"Message successfully published: {message}");
}
catch (Exception ex)
{
logger.LogError(ex, "Error occurred while publishing message to the Service Bus. Retrying after a delay.");
await Task.Delay(TimeSpan.FromSeconds(10));
await PublishMessageToServiceBus(message, logger, sessionId);
}
}
}
You can use Custom Policies to call a REST API during sign ups or sign ins which could a HTTP Triggered Azure Function.
You could also consider using API Connectors by hooking into the flow before the token is issued but note that this is currently in preview.