Example #1
0
        /// <summary>
        /// Create an ImmersiveReaderResourceConfig for the resourceKey supplied, and map the key to the ImmersiveReaderResourceConfig object.
        /// </summary>
        private void InitializeResource(IConfiguration configuration, string resourceKey)
        {
            string tenantId     = configuration[$"{resourceKey}:TenantId"];
            string clientId     = configuration[$"{resourceKey}:ClientId"];
            string clientSecret = configuration[$"{resourceKey}:ClientSecret"];
            string subdomain    = configuration[$"{resourceKey}:Subdomain"];

            if (string.IsNullOrWhiteSpace(tenantId))
            {
                throw new ArgumentNullException("TenantId is null! Did you add that info to secrets.json? See ReadMe.txt.");
            }

            if (string.IsNullOrWhiteSpace(clientId))
            {
                throw new ArgumentNullException("ClientId is null! Did you add that info to secrets.json? See ReadMe.txt.");
            }

            if (string.IsNullOrWhiteSpace(clientSecret))
            {
                throw new ArgumentNullException("ClientSecret is null! Did you add that info to secrets.json? See ReadMe.txt.");
            }

            if (string.IsNullOrWhiteSpace(subdomain))
            {
                throw new ArgumentNullException("Subdomain is null! Did you add that info to secrets.json? See ReadMe.txt.");
            }

            ResourceKeyToConfigs[resourceKey] = new ImmersiveReaderResourceConfig
            {
                TenantId     = tenantId,
                ClientId     = clientId,
                ClientSecret = clientSecret,
                Subdomain    = subdomain
            };
        }
Example #2
0
        /// <summary>
        /// Get an Azure AD authentication token using a given resource
        /// </summary>
        private async Task <string> GetTokenAsync(ImmersiveReaderResourceConfig resourceConfig)
        {
            string       authority = $"https://login.windows.net/{resourceConfig.TenantId}";
            const string resource  = "https://cognitiveservices.azure.com/";

            AuthenticationContext authContext      = new AuthenticationContext(authority);
            ClientCredential      clientCredential = new ClientCredential(resourceConfig.ClientId, resourceConfig.ClientSecret);

            AuthenticationResult authResult = await authContext.AcquireTokenAsync(resource, clientCredential);

            return(authResult.AccessToken);
        }