Esempio n. 1
0
        private async Task <Subscription> CreateSubscriptionWithResource(string resource)
        {
            if (string.IsNullOrEmpty(resource))
            {
                return(null);
            }
            var graphServiceClient = GetGraphClient();

            if (Subscriptions.Any(s => s.Value.Resource == resource))
            {
                return(null);
            }
            IGraphServiceSubscriptionsCollectionPage existingSubscriptions = null;

            try
            {
                existingSubscriptions = await graphServiceClient
                                        .Subscriptions
                                        .Request().
                                        GetAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"CreateNewSubscription-ExistingSubscriptions-Failed: {resource}");
                return(null);
            }
            var notificationUrl      = _config["BaseUrl"] + "/api/notifications";
            var existingSubscription = existingSubscriptions.FirstOrDefault(s => s.Resource == resource);

            if (existingSubscription != null && existingSubscription.NotificationUrl != notificationUrl)
            {
                _logger.LogWarning($"CreateNewSubscription-ExistingSubscriptionFound: {resource}");
                DeleteSubscription(existingSubscription);
                existingSubscription = null;
            }
            if (existingSubscription == null)
            {
                var sub = new Subscription
                {
                    Resource                 = resource,
                    ChangeType               = "created",
                    NotificationUrl          = notificationUrl,
                    ClientState              = Guid.NewGuid().ToString(),
                    ExpirationDateTime       = DateTime.UtcNow + new TimeSpan(days: 0, hours: 0, minutes: 10, seconds: 0),
                    IncludeProperties        = false,
                    LifecycleNotificationUrl = _config["BaseUrl"] + "/api/webhookLifecyle",
                    AdditionalData           = new Dictionary <string, object>()
                    {
                        ["includeResourceData"]     = true,
                        ["encryptionCertificate"]   = _config["SelfSignedCert"],
                        ["encryptionCertificateId"] = "readchannelmessage",
                    }
                };
                try
                {
                    existingSubscription = await graphServiceClient
                                           .Subscriptions
                                           .Request()
                                           .AddAsync(sub);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, $"CreateNewSubscription-Failed: {resource}");
                    return(null);
                }
            }
            Subscriptions[existingSubscription.Id] = existingSubscription;
            _logger.LogWarning($"Subscription Created for TeamId: {resource}");
            return(existingSubscription);
        }
        private async Task <Subscription> CreateSubscriptionWithResource(string resource)
        {
            if (string.IsNullOrEmpty(resource))
            {
                return(null);
            }

            var graphServiceClient = GetGraphClient();

            if (Subscriptions.Any(s => s.Value.Resource == resource && s.Value.ExpirationDateTime < DateTime.UtcNow))
            {
                return(null);
            }

            IGraphServiceSubscriptionsCollectionPage existingSubscriptions = null;

            try
            {
                existingSubscriptions = await graphServiceClient
                                        .Subscriptions
                                        .Request().
                                        GetAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"CreateNewSubscription-ExistingSubscriptions-Failed: {resource}");
                return(null);
            }

            var notificationUrl = _config["BaseUrl"] + "/api/notifications";

            var existingSubscription = existingSubscriptions.FirstOrDefault(s => s.Resource == resource);

            if (existingSubscription != null && existingSubscription.NotificationUrl != notificationUrl)
            {
                _logger.LogWarning($"CreateNewSubscription-ExistingSubscriptionFound: {resource}");
                await DeleteSubscription(existingSubscription);

                existingSubscription = null;
            }
            if (existingSubscription == null)
            {
                var sub = new Subscription
                {
                    Resource                  = resource,
                    ChangeType                = "updated",
                    NotificationUrl           = notificationUrl,
                    ClientState               = "ClientState",
                    ExpirationDateTime        = DateTime.UtcNow + new TimeSpan(days: 0, hours: 0, minutes: SubscriptionExpirationTimeInMinutes, seconds: 0),
                    LatestSupportedTlsVersion = "v1_2",
                };

                try
                {
                    existingSubscription = await graphServiceClient
                                           .Subscriptions
                                           .Request()
                                           .AddAsync(sub);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, $"CreateNewSubscription-Failed: {resource}");
                    return(null);
                }
            }

            Subscriptions[existingSubscription.Id] = existingSubscription;

            _logger.LogWarning($"Subscription Created for TeamId: {resource}");

            return(existingSubscription);
        }