public override void Load()
 {
     if (keyVaultKeys.Any())
     {
         foreach (var keyVaultKey in keyVaultKeys)
         {
             var keyVaultSecret = keyVaultService.GetSecretAsync(keyVaultKey).GetAwaiter().GetResult();
             Data.Add(keyVaultKey, keyVaultSecret);
         }
     }
 }
Пример #2
0
        public void KeyVaultConfigurationProviderWhenBuiltWithKeysBuildsProvider()
        {
            //Arrange
            A.CallTo(() => keyVaultService.GetSecretAsync(A <string> .Ignored)).Returns("a-secret-value");
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton <IKeyVaultService>(keyVaultService);

            var configurationBuilder = new ConfigurationBuilder().AddKeyVaultConfigurationProvider(new List <string>()
            {
                "a", "b", "c", "d"
            }, serviceCollection.BuildServiceProvider());

            //Act
            var configuration = configurationBuilder.Build();

            //Assert
            A.CallTo(() => keyVaultService.GetSecretAsync(A <string> .Ignored)).MustHaveHappened(4, Times.Exactly);
            Assert.Equal("a-secret-value", configuration["a"]);
            Assert.Equal("a-secret-value", configuration["b"]);
            Assert.Equal("a-secret-value", configuration["c"]);
            Assert.Equal("a-secret-value", configuration["d"]);
        }
Пример #3
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "checkhealth")] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("CheckHeallth method called.");

            var keyVaultName = _config["KeyVaultName"];

            if (string.IsNullOrWhiteSpace(keyVaultName))
            {
                return(new BadRequestObjectResult("Missing KeyVaultName setting"));
            }

            var keyVaultSecret = await _keyVaultService.GetSecretAsync("Secret1");

            if (string.IsNullOrWhiteSpace(keyVaultSecret))
            {
                return(new BadRequestObjectResult("Missing Secret1 setting"));
            }

            var aisKey = await _keyVaultService.GetSecretAsync("APPINSIGHTS_INSTRUMENTATIONKEY");

            if (string.IsNullOrWhiteSpace(aisKey))
            {
                return(new BadRequestObjectResult("Missing APPINSIGHTS_INSTRUMENTATIONKEY setting"));
            }

            var canConnect = await _dbContext.Database.CanConnectAsync();

            if (canConnect == false)
            {
                return(new BadRequestObjectResult("Cannot connect to database"));
            }

            return(new OkObjectResult("SUCCESS"));
        }
Пример #4
0
        public async Task <List <CroppedImage> > AnalyzeImageAsync(IFormFile imageFile, double probability)
        {
            var stream = imageFile.OpenReadStream();
            int height = 0;
            int width  = 0;

            string url = $"https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Prediction/{ await _keyVaultService.GetSecretAsync("CognitiveServiceKey1") }/image?iterationId={ await _keyVaultService.GetSecretAsync("IterationId") }";

            byte[] data;
            var    result        = new PredictionResultViewModel();
            var    croppedImages = new List <CroppedImage>();

            using (var client = new HttpClient())
                using (var br = new BinaryReader(stream))
                {
                    data = br.ReadBytes((int)stream.Length);
                    var image  = Image.FromStream(stream);
                    var bitmap = new Bitmap(image);

                    height = bitmap.Height;
                    width  = bitmap.Width;

                    if (data.Length > 0)
                    {
                        using (var content = new ByteArrayContent(data))
                        {
                            content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                            client.DefaultRequestHeaders.Add("Prediction-Key", await _keyVaultService.GetSecretAsync("PredictionKey"));

                            using (var response = await client.PostAsync(url, content))
                            {
                                result = await response.Content.ReadAsAsync <PredictionResultViewModel>();

                                result.OriginalHeight = height;
                                result.OriginalWidth  = width;

                                return(await CropImageAsync(bitmap, result, probability / 100d));
                            }
                        }
                    }
                }

            return(new List <CroppedImage>());
        }
Пример #5
0
        private async Task <BlobContainerClient> CreateBackupBlobClient()
        {
            if (_accountName.Equals("devstoreaccount1"))
            {
                StorageSharedKeyCredential credentials = new StorageSharedKeyCredential(_accountName, _accountKey);
                BlobServiceClient          client      = new BlobServiceClient(new Uri(_blobEndpoint), credentials);
                return(client.GetBlobContainerClient("backup"));
            }

            string backupAccountKey = await _keyVaultService.GetSecretAsync(
                _keyVaultUri,
                "AzureStorageConfiguration--BackupAccountKey");

            StorageSharedKeyCredential storageCredentials = new StorageSharedKeyCredential(string.Format(_accountName, _environment), backupAccountKey);
            Uri storageUrl = new Uri(string.Format(_accountEndpoint, _environment));

            BlobServiceClient   commonBlobClient    = new BlobServiceClient(storageUrl, storageCredentials);
            BlobContainerClient blobContainerClient = commonBlobClient.GetBlobContainerClient("backup");

            return(blobContainerClient);
        }
        public async Task <OpticalCharacterRecognitionResultViewModel> AnalyzeImageAsync(IFormFile imageFile)
        {
            var stream = imageFile.OpenReadStream();

            using (var br = new BinaryReader(stream))
            {
                var byteData = br.ReadBytes((int)stream.Length);

                if (byteData.Length > 0)
                {
                    using (var client = new HttpClient())
                        using (var data = new ByteArrayContent(byteData))
                        {
                            data.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", await _keyVaultService.GetSecretAsync("CognitiveServiceKey2"));

                            using (HttpResponseMessage response = await client.PostAsync("https://southeastasia.api.cognitive.microsoft.com/vision/v1.0/ocr?language=en", data))
                            {
                                using (HttpContent content = response.Content)
                                {
                                    if (response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCode.Accepted)
                                    {
                                        string jsonResponse = await content.ReadAsStringAsync();

                                        return(JsonConvert.DeserializeObject <OpticalCharacterRecognitionResultViewModel>(jsonResponse));
                                    }
                                }
                            }
                        }
                }
            }

            return(null);
        }
        public async Task <HandwrittenResultViewModel> AnalyzeImageAsync(byte[] byteData)
        {
            if (byteData.Length > 0)
            {
                using (var client = new HttpClient())
                    using (var data = new ByteArrayContent(byteData))
                    {
                        data.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", await _keyVaultService.GetSecretAsync("CognitiveServiceKey2"));

                        using (var response = await client.PostAsync("https://southeastasia.api.cognitive.microsoft.com/vision/v1.0/recognizeText?handwriting=true", data))
                        {
                            using (var content = response.Content)
                            {
                                if (response.StatusCode == System.Net.HttpStatusCode.Accepted)
                                {
                                    bool isAnalizying = true;
                                    do
                                    {
                                        var textOperation = response.Headers.GetValues("Operation-Location").FirstOrDefault();

                                        var result = await client.GetAsync(textOperation);

                                        string jsonResponse = result.Content.ReadAsStringAsync().Result;

                                        var handwrittenAnalyzeResult = JsonConvert.DeserializeObject <HandwrittenResultViewModel>(jsonResponse);

                                        isAnalizying = handwrittenAnalyzeResult.Status != "Succeeded";

                                        if (!isAnalizying)
                                        {
                                            return(handwrittenAnalyzeResult);
                                        }
                                    } while (isAnalizying);
                                }
                            }
                        }
                    }
            }

            return(null);
        }