public async Task <IDictionary <string, string> > EnumerateTenantsIdAndNameAccessibleByUser(IEnumerable <string> tenantIds, Func <string, Task <string> > getTokenForTenant)
        {
            Dictionary <string, string> tenantInfo = new Dictionary <string, string>();

            foreach (string tenantId in tenantIds)
            {
                string displayName;
                try
                {
                    string accessToken = await getTokenForTenant(tenantId);

                    httpClient.DefaultRequestHeaders.Remove("Authorization");
                    httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");

                    var httpResult = await httpClient.GetAsync(GraphTenantInfoUrl);

                    var json = await httpResult.Content.ReadAsStringAsync();

                    OrganizationResult organizationResult = JsonSerializer.Deserialize <OrganizationResult>(json);
                    displayName = organizationResult.value.First().displayName;
                }
                catch
                {
                    displayName = "you need to sign-in (or have the admin consent for the app) in that tenant";
                }

                tenantInfo.Add(tenantId, displayName);
            }
            return(tenantInfo);
        }
Exemplo n.º 2
0
        public async Task <IDictionary <string, string> > EnumerateTenantsIdAndNameAccessibleByUser(IEnumerable <string> tenantIds, Func <string, Task <string> > getTokenForTenant)
        {
            Dictionary <string, string> tenantInfo = new Dictionary <string, string>();

            foreach (string tenantId in tenantIds)
            {
                string displayName;
                try
                {
                    string accessToken = await getTokenForTenant(tenantId);

                    httpClient.DefaultRequestHeaders.Remove("Authorization");
                    httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");

                    var httpResult = await httpClient.GetAsync(GraphTenantInfoUrl);

                    var json = await httpResult.Content.ReadAsStringAsync();

                    OrganizationResult organizationResult = JsonConvert.DeserializeObject <OrganizationResult>(json);
                    displayName = organizationResult.value.First().displayName;
                }
                catch (Exception e)
                {
                    displayName = e.ToString();
                }

                tenantInfo.Add(tenantId, displayName);
            }
            return(tenantInfo);
        }