Exemple #1
0
        public async Task <Core.Models.Tenant> Tenant(AzureAccessToken token)
        {
            var msg = new HttpRequestMessage()
                      .Get($"https://graph.microsoft.com/v1.0/{token.TenantId}/organization/")
                      .WithHeader("Authorization", $"Bearer {token.Value}");
            var response = await client.SendRequest(msg).ConfigureAwait(false);

            // https://stackoverflow.com/questions/43301218/authenticating-with-azure-active-directory-on-powershell
            // also seems to indicate no subscription (aka useless)
            if (!response.IsSuccessStatusCode)
            {
                throw new UnauthorizedAccessException(token.TenantId);
            }
            var body = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            var marker = "\"value\":";
            var json   = body.Substring(body.IndexOf(marker) + marker.Length);

            json = json.Substring(0, json.Length - 1);
            var tenants = JsonConvert.DeserializeObject <IEnumerable <Core.AzureRM.Models.Tenant> >(json);
            var arm     = tenants.Single(t => t.Id == token.TenantId);

            return(new Core.Models.Tenant
            {
                DefaultVerifiedDomain = arm.VerifiedDomains.Single(x => x.IsDefault).Name,
                DisplayName = arm.DisplayName,
                Id = arm.Id
            });
        }
Exemple #2
0
        public static WebClient AddAuthHeader(this WebClient webClient, CRMAuthTokenConfiguration crmAuthInfo)
        {
            AzureAccessToken token = GetAccessToken(crmAuthInfo);

            webClient.Headers[HttpRequestHeader.Authorization] = $"{token.token_type} {token.access_token}";
            return(webClient);
        }
Exemple #3
0
        public static HttpClient AddAuthHeader(this HttpClient httpClient, CRMAuthTokenConfiguration crmAuthInfo)
        {
            AzureAccessToken token = GetAccessToken(crmAuthInfo);

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", $"{token.token_type} {token.access_token}");
            return(httpClient);
        }
Exemple #4
0
        AzureAccessToken AddContext(AuthenticationResult authentication)
        {
            var ctx = new AzureAccessToken
            {
                TenantId = authentication.TenantId,
                Value    = authentication.AccessToken
            };

            contexts.Add(ctx);
            return(ctx);
        }
Exemple #5
0
        public static AzureAccessToken GetAccessToken(CRMAuthTokenConfiguration crmAuthInfo)
        {
            AzureAccessToken token = null;

            using (WebClient client = new WebClient())
            {
                string oauthUrl = $"https://login.microsoftonline.com/{crmAuthInfo.TenantId}/oauth2/token";
                string reqBody  = $"grant_type=client_credentials&client_id={Uri.EscapeDataString(crmAuthInfo.ClientApplicationId)}&client_secret={Uri.EscapeDataString(crmAuthInfo.ClientSecret)}&resource={Uri.EscapeDataString(crmAuthInfo.ResourceId)}";

                client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                string response = client.UploadString(oauthUrl, reqBody);
                token = JsonHelper.Deserialize <AzureAccessToken>(response);
            }
            return(token);
        }
Exemple #6
0
        public async Task <IEnumerable <TenantIdentifier> > AvailableTenants(AzureAccessToken token)
        {
            var msg = new HttpRequestMessage()
                      .Get($"{ArmBaseUrl}/tenants?api-version=2016-06-01")
                      .WithHeader("Authorization", $"Bearer {token.Value}");
            var response = await client.SendRequest(msg).ConfigureAwait(false);

            var body = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            var json = body.Substring(9);

            json = json.Substring(0, json.Length - 1);
            var data = JsonConvert.DeserializeObject <IEnumerable <TenantIdentifier> >(json);

            return(data);
        }
Exemple #7
0
        public async Task <IEnumerable <Subscription> > Subscriptions(AzureAccessToken token)
        {
            var msg = new HttpRequestMessage()
                      .Get($"{ArmBaseUrl}/subscriptions?api-version=2016-06-01")
                      .WithHeader("Authorization", $"Bearer {token.Value}");
            var response = await client.SendRequest(msg).ConfigureAwait(false);

            var body = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            var marker = "\"value\":";
            var json   = body.Substring(body.IndexOf(marker) + marker.Length);

            json = json.Substring(0, json.Length - 1);
            var subs = JsonConvert.DeserializeObject <IEnumerable <Subscription> >(json);

            return(subs);
        }