Share via

AzureFunction webhook does not return notifications

t0 1 Reputation point
2022-01-27T15:45:38.713+00:00

Good day folks.

The issue I encounter is as follows:
I do not get any notification on any changes whatsoever.
Here's my Webhook:

public class NotificationHook
{
    private readonly AppSettings _settings;
    private IGraphClientService _graphClientService;
    private Subscription _subscription;

    public GraphNotificationHook(IOptions<AppSettings> options, IGraphClientService graphClientService)
    {
        this._settings = options.Value;
        this._graphClientService = graphClientService;
     }

    [FunctionName("Notification_Webhook")]
    public static async Task<IActionResult> SetupGraphHook(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
    [DurableClient] IDurableOrchestrationClient client,
    ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        var validationToken = req.Query["validationToken"];
        log.LogInformation($"Received Token: {validationToken}");

        // parse query parameter
        if (!string.IsNullOrEmpty(req.Query["validationToken"]))
        {
            log.LogInformation($"Received Token: {validationToken}");
            return new ContentResult { Content = validationToken, ContentType = "text/plain" };
        }
        using (StreamReader reader = new StreamReader(req.Body))
        {
            string content = await reader.ReadToEndAsync();      
            Notifications notifications = JsonConvert.DeserializeObject<Notifications>(content);

//exception thrown here as "notifications" is null
            /*
                if (!notifications.value.FirstOrDefault().ClientState.Equals("Test", StringComparison.OrdinalIgnoreCase))   
                    return new BadRequestResult();
            */

 //does not execute as notifications is null
            if (notifications != null)
                foreach (var notification in notifications.Items)
                    log.LogInformation($"Received notification: '{notification.Resource}', {notification.ResourceData?.Id}, {notification.SubscriptionId}");
        }
        return new OkResult();
    }
}

And here is how I create the subscription from inside the azure function:

[FunctionName("Start_Service")]
public static async Task<IActionResult> StarterService(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
[DurableClient] IDurableOrchestrationClient client,
ILogger log)
{
    await client.StartNewAsync("Create_Subscription");
    return new OkResult();
}

[FunctionName("Create_Subscription")]
public async Task<IActionResult> CreateSubscription(
[OrchestrationTrigger] IDurableOrchestrationContext context,
ILogger log)
{
    var subscription = new Subscription
    {
        ChangeType = "updated",
        NotificationUrl = $"{_settings.Ngrok}" + "/api/Notification_Webhook",
        Resource = $"groups/{_settings.Group_Id}" + "/members",
        ExpirationDateTime = DateTime.UtcNow.AddMinutes(15),
        ClientState = $"Test",
        //LatestSupportedTlsVersion = "v1_0"
    };

    var newSubscription = await context.CallActivityAsync<Subscription>("Authorize_Subscription", subscription);

    this._subscription = newSubscription;

    log.LogInformation($"Subscription added. Id: {newSubscription.Id}, Expiration: {newSubscription.ExpirationDateTime}");
    return new OkResult();
}

[FunctionName("Authorize_Subscription")]
public async Task<Subscription> CreateSubscriptionAuth(
[ActivityTrigger] Subscription subscription, ILogger log)
{
   return await _graphClientService.GetAppGraphClient()
        .Subscriptions
        .Request()
        .AddAsync(subscription);
}

Console Output:

Host lock lease acquired by instance ID '00000000000000000000000031BB71AF'.
Executing 'Notification_Webhook' (Reason='This function was programmatically called via the host APIs.', Id=82753e63-d6c6-475f-91c9-27d4776f5b4a)
C# HTTP trigger function processed a request.
Received Token:
Received Token:
Executed 'Notification_Webhook' (Succeeded, Id=82753e63-d6c6-475f-91c9-27d4776f5b4a, Duration=222ms)

Executing 'Start_Service' (Reason='This function was programmatically called via the host APIs.', Id=fbd63b1b-4dd4-4c26-8872-90975da80bf0)
Executed 'Start_Service' (Succeeded, Id=fbd63b1b-4dd4-4c26-8872-90975da80bf0, Duration=1355ms)
Executing 'Create_Subscription' (Reason='(null)', Id=7062303c-e04g-4d20-b50c-fa05ab5fb321)
Executed 'Create_Subscription' (Succeeded, Id=7062303c-e04g-4d20-b50c-fa05ab5fb321, Duration=304ms)
Executing 'Authorize_Subscription' (Reason='(null)', Id=859267fc-a011-4214-90c4-4d0fe3be18ea)
Executing 'Notification_Webhook' (Reason='This function was programmatically called via the host APIs.', Id=a4714942-a6ed-43cc-99fd-f9b0ea3f1e4b)
C# HTTP trigger function processed a request.
Received Token: Validation: Testing client application reachability for subscription Request-Id: 74da08aa2-d81e-4e97-92a8-c21051a1b1af
Received Token: Validation: Testing client application reachability for subscription Request-Id: 74da08aa2-d81e-4e97-92a8-c21051a1b1af
Executed 'Notification_Webhook' (Succeeded, Id=a4714942-a6ed-66cc-99fd-f9b0ea3f1e4b, Duration=43ms)
Executed 'Authorize_Subscription' (Succeeded, Id=859267fc-a011-4214-90c4-4d0fe3be18ea, Duration=6580ms)
Executing 'Create_Subscription' (Reason='(null)', Id=ccaf43b1-6909-48af-8bbd-b1c08b49f716)
Subscription added. Id: b8e73170-7d14-49df-acc2-fb6f7e21bd7c, Expiration: 2022-01-27 3:50:56 PM +00:00
Executed 'Create_Subscription' (Succeeded, Id=ccaf43b1-6909-48af-8bbd-b1c08b49f716, Duration=40ms)

But as stated above, no notifications appear whatsoever and the corresponding deserialized objects are null aswell as the request body being empty

Azure Functions
Azure Functions

An Azure service that provides an event-driven serverless compute platform.

Microsoft Security | Microsoft Graph

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.