public async Task ApplyAsync(HttpRequestMessage req)
        {
            var token = await _provider.GetBearerTokenAsync(_tokenProviderConfiguration.Scope);

            if (token.ContainsCharacters())
            {
                req.Headers.Add("Authorization", token);
            }
            req.Headers.Add("Ocp-Apim-Subscription-Key", ConfigurationManagerHelper.GetValueOnKey("subscriptionKey"));
        }
Beispiel #2
0
        /// <summary>
        /// retreives the users companies using a basic http client
        /// </summary>
        /// <returns></returns>
        public async Task <ActionResult> About()
        {
            ViewBag.Message = "Getting your companies with HttpClient";
            if (Request.IsAuthenticated)
            {
                ViewBag.Email = (User.Identity as ClaimsIdentity)?.Claims
                                .FirstOrDefault(c => c.Type == ClaimTypes.Upn)?.Value;
                var client = new HttpClient
                {
                    BaseAddress           = new Uri(ConfigurationManager.AppSettings["myApiV3Url"]),
                    DefaultRequestHeaders =
                    {
                        Authorization = AuthenticationHeaderValue.Parse(await _tokenProvider.GetBearerTokenAsync(new TokenProviderConfiguration().Scope)),
                    }
                };

                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", ConfigurationManagerHelper.GetValueOnKey("subscriptionKey"));
                var companies = await client.GetAsync("my/companies");

                ViewBag.CompaniesRawData = await companies.Content.ReadAsStringAsync();
            }
            return(View());
        }
Beispiel #3
0
 protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
 {
     request.Headers.Authorization = AuthenticationHeaderValue.Parse(await _handler.GetBearerTokenAsync(_config.Scope));
     request.Headers.Add("Ocp-Apim-Subscription-Key", _config.SubscriptionKey);
     return(await base.SendAsync(request, cancellationToken));
 }