当前位置: 首页>>代码示例>>C#>>正文


C# HttpClient.SetBearerToken方法代码示例

本文整理汇总了C#中System.Net.Http.HttpClient.SetBearerToken方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.SetBearerToken方法的具体用法?C# HttpClient.SetBearerToken怎么用?C# HttpClient.SetBearerToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Http.HttpClient的用法示例。


在下文中一共展示了HttpClient.SetBearerToken方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

		static void Main(string[] args)
		{
			var tokenClient = new TokenClient(
				"http://localhost:18942/connect/token",
				"test",
				"secret");

			//This responds with the token for the "api" scope, based on the username/password above
			var response = tokenClient.RequestClientCredentialsAsync("api1").Result;

			//Test area to show api/values is protected
			//Should return that the request is unauthorized
			try
			{
				var unTokenedClient = new HttpClient();
				var unTokenedClientResponse = unTokenedClient.GetAsync("http://localhost:19806/api/values").Result;
				Console.WriteLine("Un-tokened response: {0}", unTokenedClientResponse.StatusCode);
			}
			catch (Exception ex)
			{
				Console.WriteLine("Exception of: {0} while calling api without token.", ex.Message);
			}


			//Now we make the same request with the token received by the auth service.
			var client = new HttpClient();
			client.SetBearerToken(response.AccessToken);

			var apiResponse = client.GetAsync("http://localhost:19806/identity").Result;
			var callApiResponse = client.GetAsync("http://localhost:19806/api/values").Result;
			Console.WriteLine("Tokened response: {0}", callApiResponse.StatusCode);
			Console.WriteLine(callApiResponse.Content.ReadAsStringAsync().Result);
			Console.Read();
		}
开发者ID:dkaminski,项目名称:IdentityServer3.Samples,代码行数:34,代码来源:Program.cs

示例2: CallApi

 /// <summary>
 /// Chama a api do AppService com um token valido.
 /// </summary>
 /// <param name="token"></param>
 /// <returns></returns>
 private async Task<string> CallApi(string token) 
 {
     var client = new HttpClient();
     client.SetBearerToken(token);
     var json = await client.GetStringAsync("https://localhost:44303/identity");
     return JArray.Parse(json).ToString();
 }
开发者ID:gacalves,项目名称:GAC_ERP,代码行数:12,代码来源:CallApiController.cs

示例3: CallApi

 private static string CallApi(string token)
 {
     var client = new HttpClient();
     client.SetBearerToken(token);
     var result = client.GetStringAsync(ApplicationConstants.UrlBaseApi + "/api/test").Result;
     return result;
 }
开发者ID:umair-ali,项目名称:ThinkTecture-IdentityServer-POC-COMPLETE,代码行数:7,代码来源:HomeController.cs

示例4: GetClient

        public static HttpClient GetClient(string requestedVersion = null)
        {

            CheckAndPossiblyRefreshToken((HttpContext.Current.User.Identity as ClaimsIdentity));

            HttpClient client = new HttpClient();            

            var token = (HttpContext.Current.User.Identity as ClaimsIdentity).FindFirst("access_token");
            if (token != null)
            {
                client.SetBearerToken(token.Value);
            }

            client.BaseAddress = new Uri(ExpenseTrackerConstants.ExpenseTrackerAPI);

            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            if (requestedVersion != null)
            {
                // through a custom request header
                //client.DefaultRequestHeaders.Add("api-version", requestedVersion);

                // through content negotiation
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/vnd.expensetrackerapi.v" 
                        + requestedVersion + "+json"));
            }


            return client;
        }
开发者ID:bkcrux,项目名称:WebAPIDemo,代码行数:33,代码来源:ExpenseTrackerHttpClient.cs

示例5: CallApi

        public async Task<IActionResult> CallApi()
        {
            var accessToken = User.Claims.SingleOrDefault(claim => claim.Type == "access_token");

            if (accessToken == null)
            {
                return new BadRequestResult();
            }

            HttpClient client = new HttpClient();

            // The Resource API will validate this access token
            client.SetBearerToken(accessToken.Value);
            client.BaseAddress = new Uri(Constants.ResourceApi);

            HttpResponseMessage response = await client.GetAsync("api/Tickets/Read");

            if (!response.IsSuccessStatusCode)
            {
                return new BadRequestObjectResult("Error connecting with the API");
            }

            var responseContent = await response.Content.ReadAsStringAsync();

            return new ObjectResult(responseContent);
        }
开发者ID:softwarejc,项目名称:angular2-aspmvc-core1-getting-started,代码行数:26,代码来源:HomeController.cs

示例6: CallApi

        static void CallApi(TokenResponse response)
        {
            var client = new HttpClient();
            client.SetBearerToken(response.AccessToken);

            Console.WriteLine(client.GetStringAsync("http://localhost:44334/test").Result);
        }
开发者ID:camainc,项目名称:IdentityServer3.Samples,代码行数:7,代码来源:Program.cs

示例7: GetClient

        public static HttpClient GetClient()
        {
            if (currentClient == null)
            {
                var accessToken = App.ExpenseTrackerIdentity.Claims.First
                    (c => c.Name == "access_token").Value;

                currentClient = new HttpClient(new Marvin.HttpCache.HttpCacheHandler()
                {
                    InnerHandler = new HttpClientHandler()
                });

                currentClient.SetBearerToken(accessToken);
                           

                currentClient.BaseAddress = new Uri(ExpenseTrackerConstants.ExpenseTrackerAPI);

                currentClient.DefaultRequestHeaders.Accept.Clear();
                currentClient.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
 
            }

            return currentClient;
        }
开发者ID:bkcrux,项目名称:WebAPIDemo,代码行数:25,代码来源:ExpenseTrackerHttpClient.cs

示例8: CallApi

        static String CallApi(TokenResponse response)
        {
            var client = new HttpClient();
            client.SetBearerToken(response.AccessToken);

            return client.GetStringAsync(Constants.ApiUrl + "/test").Result;
        }
开发者ID:GreaterKudu,项目名称:angular-toolkit,代码行数:7,代码来源:Program.cs

示例9: CallApi

        private async Task<string> CallApi(string token)
        {
            var client = new HttpClient();
            client.SetBearerToken(token);

            var json = await client.GetStringAsync("http://localhost:32227/api/values");
            return json;
        }
开发者ID:pirumpi,项目名称:ssoTest,代码行数:8,代码来源:HomeController.cs

示例10: CallApi

        protected async Task<string> CallApi(string token, string url)
        {
            var client = new HttpClient();
            client.SetBearerToken(token);

            var json = await client.GetStringAsync(url);
            return JArray.Parse(json).ToString();
        }
开发者ID:mcginkel,项目名称:identityServer3Example,代码行数:8,代码来源:BaseController.cs

示例11: CallApi

        private async Task<string> CallApi(string token)
        {
            var client = new HttpClient();
            client.SetBearerToken(token);

            var json = await client.GetStringAsync(IdConstants.IdHost + "identity");
            return JArray.Parse(json).ToString();
        }
开发者ID:glennsills,项目名称:IdentityServer3.Examples,代码行数:8,代码来源:CallApiController.cs

示例12: GetUserInfos

 private async Task<String> GetUserInfos()
 {
     var user = User as ClaimsPrincipal;
     var token = user.FindFirst("access_token").Value;
     var client = new HttpClient();
     client.SetBearerToken(token);
     HttpResponseMessage response = await client.GetAsync("https://localhost:44333/core/connect/userinfo");
     return await response.Content.ReadAsStringAsync();
 }
开发者ID:fredimartins,项目名称:XYZUniversityBackend,代码行数:9,代码来源:UserInfoController.cs

示例13: CallApi

        /// <summary>
        /// Get a token before making a GET request to the specified api path
        /// </summary>
        /// <param name="route"></param>
        /// <returns></returns>
        public static string CallApi(string path)
        {
            string token = GetToken();
            //string token = GetTokenExplained();
            var client = new HttpClient();
            client.SetBearerToken(token);

            return client.GetStringAsync(ConfigurationManager.AppSettings["Api"] + "/" + path).Result;
        }
开发者ID:theNBS,项目名称:BimtoolkitApiClient,代码行数:14,代码来源:ApiAccess.cs

示例14: Main

        static void Main(string[] args)
        {
            var response = GetToken();

            var client = new HttpClient();
            client.SetBearerToken(response.AccessToken);

            var result = client.GetStringAsync("http://localhost:2025/claims").Result;
            Console.WriteLine(JArray.Parse(result));
        }
开发者ID:MichaelPetrinolis,项目名称:IdentityServer3.Samples,代码行数:10,代码来源:Program.cs

示例15: CallApiAsync

        private async Task<JArray> CallApiAsync(string token)
        {
            var client = new HttpClient();
            client.SetBearerToken(token);

            var response = await client.GetAsync("https://localhost:44347/test");
            response.EnsureSuccessStatusCode();

            var content = await response.Content.ReadAsStringAsync();
            return JArray.Parse(content);
        }
开发者ID:EdWin1409,项目名称:DotNetProIdentityServerArtikel,代码行数:11,代码来源:HomeController.cs


注:本文中的System.Net.Http.HttpClient.SetBearerToken方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。