1

Azure Event Grid的文档说

您的应用需要通过回显验证代码来做出响应。事件网格不会将事件传递给未通过验证的 WebHook 端点。

我创建了我的 WebHook Azure 函数,它可以处理任何 POST 请求,无需验证代码。尽管如此,我仍然看到我发送到此端点的自定义事件。

为什么在这种情况下不严格要求验证?

4

2 回答 2

1

令牌交换发生在 Azure Functions 的幕后,因此您无需回显验证代码。这同样适用于逻辑应用。

于 2017-10-26T07:42:30.783 回答
1

如果使用基于“事件网格触发器”的 Azure 函数作为事件订阅目标,则会自动处理订阅验证。从 2018-01-01 API 版本开始,如果您使用基于“HTTP 触发器”的函数(作为创建事件订阅期间的目标端点),您将需要在代码中处理此验证。这记录在https://docs.microsoft.com/en-us/azure/event-grid/overview

以下是 C# 中的一些示例代码:

第 1 步:向 Microsoft.Azure.EventGrid 添加依赖项:为此,请单击 Azure 函数(Azure 函数门户中最右侧的窗格)中的“查看文件”链接,然后创建一个名为 project.json 的文件。将以下内容添加到 project.json 文件并保存:

{
    "frameworks": {
        "net46": {
            "dependencies": {
               "Microsoft.Azure.EventGrid": "1.1.0-preview"
             }
         }
     }
}

第 2 步:处理订阅验证事件:

using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using Microsoft.Azure.EventGrid.Models;

class SubscriptionValidationEventData
{
    public string ValidationCode { get; set; }
}

class SubscriptionValidationResponseData
{
    public string ValidationResponse { get; set; }
}

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req,  TraceWriter log)
{
    string response = string.Empty;
    const string SubscriptionValidationEvent  = "Microsoft.EventGrid.SubscriptionValidationEvent";

    string requestContent = await req.Content.ReadAsStringAsync();
    EventGridEvent[] eventGridEvents = JsonConvert.DeserializeObject<EventGridEvent[]>(requestContent);

    foreach (EventGridEvent eventGridEvent in eventGridEvents)
    {
        JObject dataObject = eventGridEvent.Data as JObject;

        // Deserialize the event data into the appropriate type based on event type
        if (string.Equals(eventGridEvent.EventType, SubscriptionValidationEvent, StringComparison.OrdinalIgnoreCase))
        {
            var eventData = dataObject.ToObject<SubscriptionValidationEventData>();
            log.Info($"Got SubscriptionValidation event data, validation code {eventData.ValidationCode}, topic={eventGridEvent.Topic}");
            // Do any additional validation (as required) and then return back the below response
            var responseData = new SubscriptionValidationResponseData();
            responseData.ValidationResponse = eventData.ValidationCode;
            return req.CreateResponse(HttpStatusCode.OK, responseData);   
        }            
    }

    return req.CreateResponse(HttpStatusCode.OK, response);    
}
于 2018-01-31T07:23:20.340 回答