Example #1
0
        protected async Task <TokenCredentials> GetCreds()
        {
            if (hasExpired < DateTime.UtcNow.AddMinutes(-2))
            {
                var cerificateThumbprint   = CloudConfigurationManager.GetSetting("KeyVaultAuthCertThumbprint");
                var authenticationClientId = CloudConfigurationManager.GetSetting("KeyVaultAuthClientId");
                var cert          = CertificateHelper.FindCertificateByThumbprint(cerificateThumbprint);
                var assertionCert = new ClientAssertionCertificate(authenticationClientId, cert);
                var context       = new AuthenticationContext("https://login.microsoftonline.com/88c25c7a-38aa-45d5-bd8d-e939dd68c4f2");
                var result        = await context.AcquireTokenAsync(
                    "https://management.core.windows.net/", assertionCert
                    );

                accessToken = result.AccessToken;
                hasExpired  = result.ExpiresOn;
            }

            TokenCredentials creds = new TokenCredentials(accessToken);

            return(creds);
        }
Example #2
0
        private async Task RunAsync(CancellationToken cancellationToken)
        {
            var    cerificateThumbprint   = CloudConfigurationManager.GetSetting("KeyVaultAuthCertThumbprint");
            var    authenticationClientId = CloudConfigurationManager.GetSetting("KeyVaultAuthClientId");
            var    cert             = CertificateHelper.FindCertificateByThumbprint(cerificateThumbprint);
            var    assertionCert    = new ClientAssertionCertificate(authenticationClientId, cert);
            string connectionStirng = null;
            string subscriptionID   = null;

            using (var vault = new KeyVaultClient(async(authority, resource, scope) =>
            {
                var context = new AuthenticationContext(authority);
                var result = await context.AcquireTokenAsync(resource, assertionCert);
                return(result.AccessToken);
            }))
            {
                connectionStirng = (await vault.GetSecretAsync("https://stratml-keys.vault.azure.net/secrets/ServiceBusConnectionString/", cancellationToken)).Value;
                subscriptionID   = (await vault.GetSecretAsync("https://stratml-keys.vault.azure.net/secrets/SubscriptionID/", cancellationToken)).Value;
            }
            var client = new QueueClient(connectionStirng,
                                         ConfigurationManager.AppSettings["queueName"]);

            this.Queues.Clear();

            using (ServiceBusManagementClient sb = new ServiceBusManagementClient(await GetCreds())
            {
                SubscriptionId = subscriptionID
            })
            {
                IPage <SBQueue> qPage = null;
                do
                {
                    qPage = await sb.Queues.ListByNamespaceAsync("stratml", "stratml", cancellationToken);

                    this.Queues.AddRange(qPage.Select(q => q.Name));
                }while (qPage?.NextPageLink != null);
            }
            client.RegisterMessageHandler(async(msg, token) =>
            {
                var url         = Encoding.UTF8.GetString(msg.Body);
                RestClient rest = new RestClient("http://s3.amazonaws.com/irs-form-990/");
                var resp        = rest.Get(new RestRequest(url));
                XDocument doc   = XDocument.Parse(resp.Content.Replace("xsi:schemaLocation=\"http://www.irs.gov/efile\"", "").Replace(
                                                      "xmlns=\"http://www.irs.gov/efile\"", "").Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "").Replace("\r\n", ""));
                var version   = doc.Root.Attribute("returnVersion").Value.Replace(".", "");
                var type      = doc.Root.Element("ReturnHeader")?.Element("ReturnType")?.Value ?? "UNK";
                var queueName = (version + "-" + type).ToLower();
                if (!Queues.Contains(queueName))
                {
                    using (ServiceBusManagementClient sb = new ServiceBusManagementClient(await GetCreds())
                    {
                        SubscriptionId = subscriptionID
                    })
                    {
                        await sb.Queues.CreateOrUpdateAsync("stratml", "stratml", queueName, new SBQueue(), token);
                        Queues.Add(queueName);
                    }
                }
                var q = new QueueClient(connectionStirng, queueName);
                await q.SendAsync(new Message(Encoding.UTF8.GetBytes(url)));
                await q.CloseAsync();
            },
                                          new MessageHandlerOptions(evt => Task.FromException(evt.Exception))
            {
                MaxConcurrentCalls = Environment.ProcessorCount * 10, AutoComplete = true
            });
            while (!cancellationToken.IsCancellationRequested)
            {
                await Task.Delay(60000, cancellationToken);
            }
            await client.CloseAsync();
        }