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


C# HttpClient.SetBearerToken方法代码示例

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


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

示例1: CallApi

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

            Console.WriteLine(client.GetStringAsync("http://localhost:14869/test").Result);
        }
开发者ID:okusnadi,项目名称:oauth2-simple,代码行数:7,代码来源:Program.cs

示例2: CallService

 private static string CallService(string token)
 {
     var client = new HttpClient();
     client.SetBearerToken(token);
     var response = client.GetStringAsync(new Uri("http://localhost:2727/api/identity")).Result;
     return response;
 }
开发者ID:naingyelin,项目名称:Thinktecture.IdentityModel,代码行数:7,代码来源:Program.cs

示例3: CallApi

 static string CallApi(TokenResponse token, string endpoint)
 {
     Console.WriteLine(token.AccessToken);
     var client = new HttpClient();
     client.SetBearerToken(token.AccessToken);
     return client.GetStringAsync(endpoint).Result;
 }
开发者ID:mequanta,项目名称:Janitor-old,代码行数:7,代码来源:Program.cs

示例4: CallService

        private static void CallService(string token)
        {
            var client = new HttpClient();
            client.SetBearerToken(token);
            var response = client.GetStringAsync(new Uri("http://localhost:2727/api/identity")).Result;

            Console.WriteLine(response);
        }
开发者ID:Bihari,项目名称:Thinktecture.IdentityModel,代码行数:8,代码来源:Program.cs

示例5: CallService

        private static void CallService(string token)
        {
            var client = new HttpClient();
            client.SetBearerToken(Convert.ToBase64String(Encoding.UTF8.GetBytes(token)));


            var response = client.GetAsync("http://localhost:2727/api/identity").Result;
            response.EnsureSuccessStatusCode();

            Console.WriteLine(response.Content.ReadAsStringAsync().Result);

        }
开发者ID:RyanLiu99,项目名称:Thinktecture.IdentityModel,代码行数:12,代码来源:Program.cs

示例6: CallApi

        static void CallApi(TokenResponse response)
        {
            var client = new HttpClient();
            client.SetBearerToken(response.AccessToken);
            Console.WriteLine("Calling api...");
            var result = client.GetStringAsync("http://localhost:47922/test").Result;
            Console.WriteLine("API called...");
            Console.WriteLine(result);

            //WebClient client = new WebClient();
            //client.Headers["Authorization"] = "Bearer " + response.AccessToken;
            //var result = client.DownloadString("http://localhost:47922/test");
            //Console.WriteLine("Result: " + result);
        }
开发者ID:asiemer,项目名称:Microservices-Auth-Example,代码行数:14,代码来源:Program.cs

示例7: Configure

        public void Configure(IApplicationBuilder app)
        {
            app.UseCookieAuthentication(options =>
            {
                options.AuthenticationScheme = "Cookies";
                options.AutomaticAuthentication = true;
            });

            app.Map("/code", application =>
            {
                application.Use((context, next) =>
                {

                    var client = new OAuth2Client(new Uri("http://localhost:5000/core/connect/authorize"));

                    var url = client.CreateCodeFlowUrl(clientId: "0011FF", redirectUri: "http://localhost:5001/callback", scope: "read write");

                    context.Response.Redirect(url);

                    return Task.FromResult(0);

                });
            });

            app.Map("/callback", application =>
            {
                application.Use(next => async context =>
               {
                   var client = new OAuth2Client(new Uri("http://localhost:5000/core/connect/token"), "0011FF", "ABCDEFG");

                   var code = context.Request.Query["code"];

                   TokenResponse response = await client.RequestAuthorizationCodeAsync(code, "http://localhost:5001/callback");

                   if (!string.IsNullOrEmpty(response.AccessToken))
                   {
                       List<Claim> claims = new List<Claim>();
                       claims.Add(new Claim("access_token", response.AccessToken));
                       claims.Add(new Claim("expires_at", (DateTime.UtcNow.ToEpochTime() + response.ExpiresIn).ToDateTimeFromEpoch().ToString()));

                       ClaimsIdentity id = new ClaimsIdentity(claims, "cookie");
                       ClaimsPrincipal principal = new ClaimsPrincipal(id);

                       context.Response.SignIn("Cookies", principal);
                   }
               });
            });

            app.Map("/info", application =>
            {
                application.Use(next => async context =>
                 {
                     ClaimsPrincipal principal = context.User;

                     await context.Response.WriteAsync(principal.FindFirst("access_token").Value);
                 });
            });

            app.Map("/call", application =>
            {
                application.Use(next => async context =>

                {
                     HttpClient client = new HttpClient();

                     string accessToken = context.User.FindFirst("access_token").Value;

                     client.SetBearerToken(accessToken);

                     string response = await client.GetStringAsync("http://localhost:5002/action");
                    await context.Response.WriteAsync(response);

                });
            });
        }
开发者ID:sinanbabacan,项目名称:Client,代码行数:75,代码来源:Startup.cs


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