You are developing an integration solution that consumes events from an Azure Event Grid custom topic. You create an HTTP-triggered Azure Function to act as the Webhook subscription endpoint. Due to strict enterprise firewall rules, the Azure Function is hosted in a private VNet with no outbound public internet access, which prevents it from performing an asynchronous subscription validation handshake via the validation URL.
You need to implement C# code within the Azure Function to perform a synchronous subscription validation handshake when Event Grid sends the initial validation request.
Which C# code snippet should you implement inside the Azure Function to successfully complete the validation handshake?
- Aif (req.Headers.TryGetValue("Aeg-Event-Type", out var headerValue) && headerValue == "SubscriptionValidation")
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var events = JsonConvert.DeserializeObject<List<CustomValidationEvent>>(requestBody);
var code = events[0].Data.ValidationCode;
return new OkObjectResult(new { validationCode = code });
} - Bstring requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var events = JsonConvert.DeserializeObject<List<CustomValidationEvent>>(requestBody);
if (events[0].EventType == "Microsoft.EventGrid.SubscriptionValidationEvent")
{
using (var client = new HttpClient())
{
await client.GetAsync(events[0].Data.ValidationUrl);
}
return new OkResult();
} - if (req.Headers.TryGetValue("Aeg-Event-Type", out var headerValue) && headerValue == "SubscriptionValidation")
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var events = JsonConvert.DeserializeObject<List<CustomValidationEvent>>(requestBody);
var code = events[0].Data.ValidationCode;
return new OkObjectResult(new { validationResponse = code });
}Answer - Dif (req.Headers.TryGetValue("Aeg-Event-Type", out var headerValue) && headerValue == "SubscriptionValidation")
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var events = JsonConvert.DeserializeObject<List<CustomValidationEvent>>(requestBody);
var code = events[0].Data.ValidationCode;
return new OkObjectResult(code);
}
Answer
The correct code snippet checks if the 'Aeg-Event-Type' header equals 'SubscriptionValidation', reads the request body, deserializes it to retrieve the validation code from the first event's data payload, and then returns a 200 OK result with a JSON object that contains the key 'validationResponse' mapped to the validation code.
The correct response pattern checks the HTTP request header 'Aeg-Event-Type' for the value 'SubscriptionValidation'. It then deserializes the request body, extracts the validation code, and returns it synchronously in a JSON payload with the property name 'validationResponse'. This conforms to the required Azure Event Grid protocol for synchronous subscription validation.
Step-by-Step Solution
Key Concept
Azure Event Grid Webhook synchronous validation handshake requirements.