Question

Difficulty: HardImplement Azure Event Grid Solutions

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?

  1. A
    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 { validationCode = code });
    }
  2. B
    string 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();
    }
  3. 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
  4. D
    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(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

1
Inspect request headers for Event Grid handshake signatures.
Identify the 'Aeg-Event-Type' header and verify that its value is 'SubscriptionValidation'.
This confirms that the incoming POST request is a validation handshake request sent by Azure Event Grid rather than a standard event notification.
2
Read and deserialize the JSON request body payload.
Extract the 'validationCode' from the 'data' property of the first event object in the deserialized array.
Event Grid sends validation requests as an array containing a single validation event that holds the unique authorization code.
3
Construct a synchronous HTTP response containing the validation response payload.
Create a JSON object structure with the property 'validationResponse' set to the extracted validation code.
For synchronous handshakes, Event Grid expects an HTTP 200 OK response containing a JSON body structured specifically as { "validationResponse": "<validationCode>" }.

Key Concept

Azure Event Grid Webhook synchronous validation handshake requirements.
Rate this question