Пример #1
0
            public async Task <string> UploadDocument(byte[] document, string fileName)
            {
                try
                {
                    var containerName     = ConfigurationManager.AppSettings["containerName"];
                    var blobEndpoint      = ConfigurationManager.AppSettings["blobEndpoint"];
                    var blobSecurityToken = ConfigurationManager.AppSettings["blobSecurityToken"];

                    AzureKeyVaultClient _client = new AzureKeyVaultClient();
                    var token = await _client.GetSecret(blobSecurityToken);

                    var creds             = new StorageCredentials(token);
                    var accountWithSas    = new CloudStorageAccount(creds, new Uri(blobEndpoint), null, null, null);
                    var blobClientWithSas = accountWithSas.CreateCloudBlobClient();

                    var container = blobClientWithSas.GetContainerReference(containerName);
                    container.CreateIfNotExists();

                    var blob = container.GetBlockBlobReference(fileName);
                    blob.Properties.ContentType = @"image\jpeg";
                    await blob.UploadFromByteArrayAsync(document, 0, document.Length);

                    return(string.Format("{0}/{1}", containerName, fileName));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
Пример #2
0
            public async Task EnQueueMessage(SlackMessage message)
            {
                try
                {
                    var connectionString = await _client.GetSecret(queueKey);

                    QueueClient qClient = QueueClient.CreateFromConnectionString(connectionString);

                    var stringMsg = JsonConvert.SerializeObject(message);
                    var brMsg     = new BrokeredMessage(stringMsg);
                    await qClient.SendAsync(brMsg);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Exception {e.Message}");
                }
            }
        public Credentials GetCredentialsFrom(string userName)
        {
            var vault = new AzureKeyVaultClient(
                clientId: appId,
                clientSecret: secret,
                vaultName: vaultName);

            Guid id = GetAlbaAccountIdFor(userName);

            string acct = vault.GetSecret($"alba-account-name-{id}");
            string usr  = vault.GetSecret($"alba-account-user-{id}");
            string pwd  = vault.GetSecret($"alba-account-password-{id}");

            var credentials = new Credentials(acct, usr, pwd)
            {
                AlbaAccountId = id
            };

            return(credentials);
        }
Пример #4
0
            public async Task <SlackMessage> getMessages(string channelId, string messageTs)
            {
                string channelHistoryRequestUrl = ConfigurationManager.AppSettings["ChannelHistoryRequestUrl"];
                string slackTokenKeyName        = ConfigurationManager.AppSettings["SlackTokenKeyName"];
                AzureKeyVaultClient _client     = new AzureKeyVaultClient();
                var _slacktoken = await _client.GetSecret(slackTokenKeyName);


                var requestUrl = string.Concat(channelHistoryRequestUrl, $"?token={_slacktoken}&channel={channelId}&latest={messageTs}&inclusive=true&count=1");

                using (HttpClient client = new HttpClient())
                {
                    var response = await client.GetAsync(new Uri(requestUrl));

                    var content = await response.Content.ReadAsStringAsync();

                    var result = JsonConvert.DeserializeObject <Dictionary <string, object> >(content);

                    var message = ((JArray)result["messages"]).First.ToObject <Dictionary <string, object> >();

                    SlackMessage slackMessage = null;

                    if (message.ContainsKey("file"))
                    {
                        var filedetails = ((JToken)message["file"]).ToObject <Dictionary <string, object> >();
                        var ext         = filedetails["filetype"].ToString();
                        if (ext == "jpg")
                        {
                            //Retrieve File Content
                            var fileContent = await getFileContents(filedetails["url_private"].ToString());

                            //Upload file to Blob
                            var blobStorageClient = new BlobStorageClient();
                            var blobPath          = await blobStorageClient.UploadDocument(fileContent, Guid.NewGuid().ToString() + "." + ext);

                            slackMessage          = new SlackMessage();
                            slackMessage.isFile   = true;
                            slackMessage.filePath = blobPath;
                            slackMessage.text     = message["text"].ToString();
                        }
                    }
                    else
                    {
                        slackMessage        = new SlackMessage();
                        slackMessage.isFile = false;
                        slackMessage.text   = message["text"].ToString();
                    }
                    return(slackMessage);
                }
            }
Пример #5
0
            public async Task <byte[]> getFileContents(string filePath)
            {
                AzureKeyVaultClient _client = new AzureKeyVaultClient();
                var _slacktoken             = await _client.GetSecret("SlackToken");

                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _slacktoken);
                    var response = await client.GetAsync(new Uri(filePath));

                    var content = await response.Content.ReadAsByteArrayAsync();

                    return(content);
                }
            }