Ejemplo n.º 1
0
        public static async Task <bool> AddUserToStorageAsync(WHUser user, ConfigValues values)
        {
            var credentials = new StorageCredentials(values.StorageAccountName, values.StorageAccountKey);
            var blob        = new CloudBlockBlob(new Uri($"{values.StorageAccountUrl}{user.Mail}"), credentials);
            await blob.UploadFromByteArrayAsync(user.Image, 0, user.Image.Length);

            return(true);
        }
Ejemplo n.º 2
0
        public static async Task <string> AquireTokenAsync(ConfigValues values, Func <DeviceCodeResult, Task> deviceCodeResultCallback)
        {
            AuthenticationResult result = null;
            var app      = GetGraphClientApp(values.ClientID, values.Authority);
            var accounts = await app.GetAccountsAsync();

            // All AcquireToken* methods store the tokens in the cache, so check the cache firsty
            try
            {
                result = await app.AcquireTokenSilent(values.AppScopes, accounts.FirstOrDefault()).ExecuteAsync();
            }
            catch (MsalUiRequiredException ex)
            {
                // A MsalUiRequiredException happened on AcquireTokenSilent.
                // This indicates you need to call AcquireTokenInteractive to acquire a token
                System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");
            }
            try
            {
                result = await app.AcquireTokenWithDeviceCode(values.AppScopes, deviceCodeResultCallback).ExecuteAsync();

                Console.WriteLine(result.Account.Username);
                return(result.AccessToken);
            }
            catch (MsalServiceException)
            {
                // Kind of errors you could have (in ex.Message)

                // AADSTS50059: No tenant-identifying information found in either the request or implied by any provided credentials.
                // Mitigation: as explained in the message from Azure AD, the authoriy needs to be tenanted. you have probably created
                // your public client application with the following authorities:
                // https://login.microsoftonline.com/common or https://login.microsoftonline.com/organizations

                // AADSTS90133: Device Code flow is not supported under /common or /consumers endpoint.
                // Mitigation: as explained in the message from Azure AD, the authority needs to be tenanted

                // AADSTS90002: Tenant <tenantId or domain you used in the authority> not found. This may happen if there are
                // no active subscriptions for the tenant. Check with your subscription administrator.
                // Mitigation: if you have an active subscription for the tenant this might be that you have a typo in the
                // tenantId (GUID) or tenant domain name.
            }
            catch (OperationCanceledException)
            {
                // If you use a CancellationToken, and call the Cancel() method on it, then this may be triggered
                // to indicate that the operation was cancelled.
                // See https://docs.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads
                // for more detailed information on how C# supports cancellation in managed threads.
            }
            catch (MsalClientException)
            {
                // Verification code expired before contacting the server
                // This exception will occur if the user does not manage to sign-in before a time out (15 mins) and the
                // call to `AcquireTokenWithDeviceCode` is not cancelled in between
            }
            return(null);
        }
Ejemplo n.º 3
0
        public static IEnumerable <string> GetEmployees(ConfigValues values)
        {
            var storageAccount = CloudStorageAccount.Parse(values.StorageConnectionString);
            var blobClient     = storageAccount.CreateCloudBlobClient();
            var container      = blobClient.GetContainerReference(values.ContainerName);

            foreach (var c in container.ListBlobs(null, false))
            {
                if (c.GetType() == typeof(CloudBlockBlob))
                {
                    var blob = (CloudBlockBlob)c;
                    yield return(blob.Name);
                }
            }
        }
Ejemplo n.º 4
0
        public static async Task <bool> TrainModelAsync(ConfigValues values)
        {
            try
            {
                var faceClient = new FaceClient(new ApiKeyServiceClientCredentials(values.FaceApiKey), new DelegatingHandler[] { })
                {
                    Endpoint = FaceUrl
                };
                await faceClient.PersonGroup.TrainAsync(PersonGroupId);

                return(true);
            }
            catch (APIErrorException e)
            {
                Console.WriteLine(e.ToString());
                return(false);
            }
        }
Ejemplo n.º 5
0
        public static async Task <bool> AddUserToFaceApiAsync(WHUser user, ConfigValues values)
        {
            var faceClient = new FaceClient(new ApiKeyServiceClientCredentials(values.FaceApiKey), new DelegatingHandler[] { })
            {
                Endpoint = FaceUrl
            };

            await WaitCallLimitPerSecondAsync();

            using (var stream = new MemoryStream(user.Image))
            {
                try
                {
                    var existingList = await faceClient.PersonGroup.GetAsync(PersonGroupId);
                }
                catch (APIErrorException)
                {
                    try
                    {
                        await faceClient.PersonGroup.CreateAsync(PersonGroupId, "WhosHereFaces", "Faces from graph", "recognition_02");
                    }
                    catch (Exception) { }
                }
                try
                {
                    var person = await faceClient.PersonGroupPerson.CreateAsync(PersonGroupId, user.Mail, user.Mail);

                    await faceClient.PersonGroupPerson.AddFaceFromStreamAsync(PersonGroupId, person.PersonId, stream, user.Mail);
                }
                catch (APIErrorException)
                {
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 6
0
        public static async Task <IEnumerable <Person> > AnalyzeImageAsync(byte[] image, ConfigValues values)
        {
            var faceClient = new FaceClient(new ApiKeyServiceClientCredentials(values.FaceApiKey), new DelegatingHandler[] { })
            {
                Endpoint = FaceUrl
            };
            var idResult = new List <Guid>();
            var retVal   = new List <Person>();

            using (var stream = new MemoryStream(image))
            {
                var result = await faceClient.Face.DetectWithStreamAsync(stream, true, false, null, "recognition_02", true);

                var foundIds = result.Select(_ => _.FaceId.Value);

                foreach (var chunk in foundIds.Chunk(10))
                {
                    try
                    {
                        var identified = await faceClient.Face.IdentifyAsync(chunk.ToList(), PersonGroupId);

                        idResult.AddRange(identified.Where(i => i.Candidates.Any()).Select(_ => _.Candidates.First().PersonId));
                    }
                    catch (APIErrorException e)
                    {
                        Console.WriteLine(e.Body.Error.Message);
                        Console.WriteLine(e.ToString());
                    }
                }
            }
            foreach (var id in idResult)
            {
                var person = await faceClient.PersonGroupPerson.GetAsync(PersonGroupId, id);

                if (person != null)
                {
                    retVal.Add(person);
                }
            }
            return(retVal);
        }